Wednesday, August 2, 2017

Django ModelForm calls default on fields that ain't selected.

Hello,

I stumbled upon what I think is a problem.
Here is an example:

// models.py
def create_a():
    return ModelA.objects.create(name='<default-name>').id

class ModelA(models.Model):
    name = models.CharField(max_length=123)

class ModelB(models.Model):
    name = models.CharField(max_length=123)
    reference = models.ForeignKey(ModelA, default=create_a)

// views.py
class MyForm(ModelForm):
    class Meta:
        model = ModelB
        fields = ['name']

def my_view(request):
    MyForm()
    return HttpResponse('abc123')


Now every time you create an instance of MyForm the create_a function gets called which creates an instance of ModelA.
I think this is a bug or maybe I'm missing something any answers would be appreciated, Thanks.

// Jesper Jansson

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/82c8890c-b97a-4b10-9c11-6ecf05f60c83%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment