I am trying to filter a m-2-m based on some 3 way relational logic. I have the following models (example only… but the more I write it, it looks like a game I’d like to play…) Forgive my over use of the spam and eggs metavariables…
Models:
class SuperProfile(models.Model):
user = models.ForgeignKey('User')
eggs_unlocked = models.ManyToMany('Egg')
my_kitchen = models.ForeignKey('SuperKitchen')
class SuperKitchen(models.Model):
name = models.CharField(max_length=20)
class Egg(models.Model):
eggyness = models.PostiveIntegerField(help_text=_("how Eggy it is. eg."
"Spam'n'Eggs is over 9000")
kitchens = models.ForeignKey(help_text=_('What kitchen carries this')
Question: As an administrator, in someone else’s SuperProfileAdmin:
- I want to list what
eggsthey can use based on whatkitchenthey are part of - A person’s
Kitchendoesn’t change, ever. Eggsonly appear in onekitchen.- How do you get an instance of the
SuperProfileorSuperKitchento filter theEgglist for said other user
I’m not sure if this is clear, please comment on what needs clarification.
In your
EggAdmin, you must overridequerysetmethodUPDATE: Ok, that change everything… On SuperPrifile admin, when you open a SuperProfile record, you wish
eggs_unlockedto be filtered according to that user… So:I know, using regex to grab the object id is a really bad practise, but as i mention, that is the only way to do this as i know.
And here is the doc for formfield_for_manytomany