Denis
Denis

Reputation: 7343

Overriding ModelChoiceField with HiddenInput doesn't work

I have form which generates from model

class UserProfile(models.Model):
    company = models.ForeignKey(Company)
    user = models.OneToOneField(User)
    department = models.CharField(max_length=100)
    position = models.CharField(max_length=100)

class UserProfileForm(ModelForm):
    company_id = ModelChoiceField(queryset=Company.objects.all(),
                                            widget=HiddenInput())
    class Meta:
        model = UserProfile
        exclude = ('user')

But it's doesn't work, and company_id is stay visible select field. How I can create hidden field with company id ?

Upvotes: 1

Views: 796

Answers (1)

Jonas Reichert
Jonas Reichert

Reputation: 118

fieldnames between model and form should match. Use company in stead of company_id and it'll work.

Upvotes: 4

Related Questions