django-smart-selects icon indicating copy to clipboard operation
django-smart-selects copied to clipboard

Add option to allow None for the chained field

Open ekohl opened this issue 12 years ago • 4 comments
trafficstars

In our case we have customers, domain handles and domains. A handle has a customer, so does a domain. A domain has a handle (actually more, but that's not relevant now) and the choices should be limited to those relevant for that customer. For us, that's all handles that have the same customer (which ChainedForeignKey handles), but also some shared handles which have no customer.

ekohl avatar May 13 '13 16:05 ekohl

This is a little difficult to parse out. Can you post (relevant excerpts from) your models.py and show me what you're trying to do?

blag avatar Oct 20 '16 18:10 blag

Oh wow, that's a blast from the past. I no longer work at that company so I may be fuzzy on the details. I think the model was something as follows:

class Customer(Model):
    pass


class Contact(Model):
    customer = models.ForeignKey(Customer, null=True)


class Domain(Model):
    customer = models.ForeignKey(Customer)
    contact = models.ForeignKey(Contact)

Now the choices for a domain contact should be limited to domain.customer.contact_set.all() | Contact.objects.filter(customer__isnull=True).

Since I'm no longer working on that project I'd be fine with closing this issue if you think this isn't a relevant feature.

ekohl avatar Oct 20 '16 18:10 ekohl

Awesome, that makes the issue much more clear. It sounds like for each individual company (eg: Customer) you could have different people internal to that company (Contact) responsible for different domains, but you could also have people who weren't affiliated with a particular company responsible for domains. Did I correctly understand that?

Frankly, I think it's a perfectly cromulent feature request and the more general case is probably a little common so I'm happy to keep this open.

Thinking out loud here: ChainedFK or ChainedM2M can have an optional parameter that takes a Q() object that then gets ORed with the original chained query and can only be applied to the related model (in this case, Contact). Downside is now there is an (optional) second query and there isn't any related value filtering that can be done on the second parameter.

But I think if users want more advanced functionality they can use something like django-autocomplete-light to implement a custom URL/endpoint with that advanced filtering.

I'll see what I can do to make this possible. Thanks! :smiley:

blag avatar Oct 20 '16 18:10 blag

Yes, you understood it correctly.

ekohl avatar Oct 20 '16 19:10 ekohl