I am having trouble creating a filtered inline into my admin.ModelAdmin form.
I have an Org_contact model with two foreign keys to the same Contact model, based on its Contact_type_lookup. Contact_type_lookup is either 0:Person or 1:Organization. A Contact can be a Person or Organization. An Organization can have a Person as a PoC which is maintained in Org_contact. (this is a pre-existing db)
models.py
class Org_contact(models.Model):
org_contact_id = models.AutoField(primary_key=True, blank=False, null=False)
person_id = models.ForeignKey('Contact', related_name = 'person_id', blank=False, null=False) # related name because two fields linked to same table
org_id = models.ForeignKey('Contact', related_name= 'org_id', blank=False, null=False) # related name because two fields linked to same table
class Contact(models.Model):
contact_id = models.AutoField(primary_key=True, blank=False, null=False)
first_name = models.CharField(max_length=500, blank=True, null=True)
last_name = models.CharField(max_length=500, blank=True, null=True)
contact_type_lookup = models.ForeignKey('Contact_type_lookup', to_field="contact_type_id",
db_column="contact_type", blank=False, null=False)
class Contact_type_lookup(models.Model):
contact_type_id = models.AutoField(primary_key=True, blank=False, null=False)
contact_type = models.CharField(max_length=500, blank=False, null=False)
def __str__(self):
return u'%s' % self.contact_type
The Org_contact model links a POC Person to an Organization. I want this as an inline when editing or creating a Contact. My thought was to have two inlines, one that would reveal only filtered Persons when the user selects Contacts.contact_type_lookup = 1 on the form and another inline that reveals only filtered Organizations when the Contacts.contact_type_lookup = 0. My plan was to hide or show these inlines based on the selection of contact_type_lookup.
admin.py
class PocPersonInline(admin.TabularInline):
model = Org_contact
formset = PersonInlineFormSet
fk_name = 'person_id'
extra = 1
class PocOrgInline(admin.TabularInline):
model = Org_contact
formset = OrgInlineFormSet
fk_name = 'org_id'
extra = 1
@admin.register(Contact)
class ContactAdmin(admin.ModelAdmin):
include=['last_name', 'first_name', 'contact_type_lookup',]
form = AddContactForm
...
inlines = [PocPersonInline, PocOrgInline]
I tried to extend a BaseInlineFormSet from this example: [Django admin, filter objects for inline formset
So I have:
class PersonInlineFormSet(BaseInlineFormSet):
def get_queryset(self):
if not hasattr(self, '_queryset'):
qs = super(PersonInlineFormSet, self).get_queryset()
#filter
self._queryset = qs.filter(person_id__contact__contact_type_lookup = 0) #Error: Relation fields do not support nested lookups
return self._queryset
But I don't understand how to filter the Org_contact inline. If I don't filter, the inline shows all the rows in Contact. But for this inline I only want to show the rows of Contact where contact_type_lookup = 0.
The filter does not support nested lookups.
How can I create an inline where the choices are only Contact objects where contact_type_lookup = 0?
Aucun commentaire:
Enregistrer un commentaire