KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have these models (I have limited the number of fields to just those needed) class unit(models.Model): name = models.CharField(max_length=200) class project(models.Model): name = models.CharField(max_length=200) class location(address): project = models.ForeignKey(project) class project_unit(models.Model): project = models.ForeignKey(project) unit = models.ForeignKey(unit) class location_unit(models.Model): project = models.ForeignKey(project) #Limit the selection of locations based on which project has been selected location = models.ForeignKey(location) #The same here for unit. But I have no idea how. unit = models.ForeignKey(project_unit) My newbie head just cannot grasp how to limit the two fields, location and unit, in the location_unit model to only show the choices which refers to the selected project in location_unit. Should I override the modelform and make a query there or can I use the limit_choices_to. Either way I have failed trying both Edit: Just to clarify, I want this to happen in the Django Admin. I have also tried formfield_for_foreignkey, but still a no go for me. EDIT 2: def formfield_for_foreignkey(self, db_field, request, **kwargs): if db_field.name == "unit": kwargs["queryset"] = project_unit.objects.filter(project=1) return db_field.formfield(**kwargs) return super(location_unit_admin, self).formfield_for_foreignkey(db_field, request, **kwargs) The above code snippet works. But of course I don't want the project to point to 1. How do I reference to the models project_id? I tried this: kwargs["queryset"] = project_unit.objects.filter(project=self.model.project.project_id) But that doesn't work (actually I have tried a lot of variations, yes I am a django newbie)
Tags (comma-separated)
Save Edits
Cancel