开发者

Django form: Unique field

I have 2 simple form/model classes

class Booking(forms.Form):  
    name = models.CharField(max_length=100, verbose_name="Your name开发者_JS百科*:")  
    place = models.ManyToManyField(Location, blank=True, null=True)

class Location(models.Model):
    place = models.CharField(max_length=100)

When I display the form I only want to show locations not already previously picked. The tricky bit (I think) is having the location as ManytoManyField as I can't add unique=True to it.

So for example user x will pick from a list (London, Cardiff or Edinburgh) and select London. When user y loads the form London will no longer be available to select.

Any ideas?


How about controlling this at the model level? Add a BooleanField, called 'inuse', or something similar, to the Location model, and then you can add a filter to your form that only selects the ones that aren't in use.

You would flip inuse to True when User x submits their form...

Also, you could potentially change the relationship to ForeignKey, and then you could use 'unique'

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜