1

I have a model that looks like this:

class Client(models.Model):
name = models.CharField(max_length=100, primary_key=True)
user = models.ForeignKey(User)       

class Contract(models.Model):                   
    title = models.CharField(max_length=100, primary_key=True)
    start_date = models.DateField()
    end_date = models.DateField()
    description = models.TextField()   
    client = models.ForeignKey(Client)  
    user = models.ForeignKey(User)     

How can i configure a django form so that only clients associated with that user show in the field in the form? My initial thought was this in my forms.py:

    client = forms.ModelChoiceField(queryset=Client.objects.filter(user__username = User.username))  

But it didn't work. So how else would I go about it?

Dean
  • 8,668
  • 17
  • 57
  • 86

1 Answers1

2

Creating a dynamic choice field

Community
  • 1
  • 1
Arnaud
  • 1,785
  • 18
  • 22
  • I'm now getting a UnboundLocalError, local variable 'contractForm' referenced before assignment on this line in my view: contractForm = contractForm(request.user) – Dean Feb 13 '11 at 23:46
  • Isn't the form class `ContractForm`, with a capital C? It should probably read `contractForm = ContractForm(request.user)`. – Arnaud Feb 13 '11 at 23:50
  • That got it now on to another error. It's saying that __init__() takes at least 2 non-keyword arguments (1 given) – Dean Feb 13 '11 at 23:58
  • It's hard to say without seeing the code. Can you edit your question to show us what you have so far? – Arnaud Feb 14 '11 at 00:00
  • Fixed that error. Now on to why the for isn't rendering. The last error was because I was using forms.ChoiceField instead of forms.ModelChoiceField – Dean Feb 14 '11 at 00:02