views:

34

answers:

1

I have 2 simple form/model classes

class Booking(forms.Form):  
    name = models.CharField(max_length=100, verbose_name="Your name*:")  
    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?

A: 

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'

nstehr
wow no idea how I would do that. Would I filter on the model or the view?
Rob B
I did it previously in the view. I had a field in my form called 'coach' and I wanted to populate its drop down with all the users who were in the 'coach' user group I created. Something like this: form.fields['coach'].queryset = User.objects.all().filter(groups__name='coach')
nstehr