Tuesday, January 31, 2017

Limiting choices in a lookup by exclusion


I have this code, and with Django 1.10 it works as expected:

class Location(models.Model):
    name = models.CharField(max_length = 32, unique = True)
    street = models.CharField(max_length = 32)
    detail = models.CharField(max_length = 32, blank = True, null = True)
    city = models.CharField(max_length = 32)
    state = USStateField()
    zip = USZipCodeField()
    phone = models.ManyToManyField(Phone, limit_choices_to = {'type_id':'H'})

but what I'd like to do is restrict the choices of phone numbers for Locations to those that aren't 'C' (cell phones) - I've found some hints, but they're all for older versions of Django - it seems like:

    phone = models.ManyToManyField(Phone, limit_choices_to = {'type_id__ne':'C'})

ought to work - but this provokes a TypeError:

Related Field got invalid lookup: ne

Thanks!

No comments:

Post a Comment