KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have two models related by a foreign key: # models.py class TestSource(models.Model): name = models.CharField(max_length=100) class TestModel(models.Model): name = models.CharField(max_length=100) attribution = models.ForeignKey(TestSource, null=True) By default, a django ModelForm will present this as a <select> with <option> s; however I would prefer that this function as a free form input, <input type="text"/> , and behind the scenes get or create the necessary TestSource object and then relate it to the TestModel object. I have tried to define a custom ModelForm and Field to accomplish this: # forms.py class TestField(forms.TextInput): def to_python(self, value): return TestSource.objects.get_or_create(name=value) class TestForm(ModelForm): class Meta: model=TestModel widgets = { 'attribution' : TestField(attrs={'maxlength':'100'}), } Unfortunately, I am getting: invalid literal for int() with base 10: 'test3' when attempting to check is_valid on the submitted form. Where am I going wrong? Is their and easier way to accomplish this?
Tags (comma-separated)
Save Edits
Cancel