Django - Form Validation Error

I have a model :

class Entity(models.Model):    
    entity_name = models.CharField(max_length=100)
    entity_id = models.CharField(max_length=30, primary_key=True)
    entity_parent = models.CharField(max_length=100, null=True)
    photo_id = models.CharField(max_length=100, null=True)
    username = models.CharField(max_length=100, null=True)
    date_matched_on = models.CharField(max_length=100, null=True)
    status = models.CharField(max_length=30, default="Checked In")

    def __unicode__(self):
        return self.entity_name

    class Meta:
        app_label = 'match'
        ordering = ('entity_name','date_matched_on')
        verbose_name_plural='Entities'

I also have a view :

def photo_match(request):
''' performs an update in the db when a user chooses a photo '''

    form = EntityForm(request.POST)
    form.save()

And my EntityForm looks like this:

class EntityForm(ModelForm):
    class Meta:
        model = Entity

My template form returns POST back to the view with the following values:
 {u'username ': [u'admin'], u'entity_parent ': [u'PERSON'], u'entity_id ': [u'152097'], u 'photo_id': [u'2200734 '], u' entity_name ': [u'AJ McLean'], u'status ': [u'Checked Out], u'date_matched_on': [u'5 / 20/2010 10 : 57 AM ']}

And form.save () throws this error:

Exception in photo_match: object cannot be modified because data is not verified.

, , . . - , !

,

+3
1

, , , , save INSERT, UPDATE, ( django docs ).

Try:

def photo_match(request):
''' performs an update in the db when a user chooses a photo '''

    entity = Entity.objects.get(pk=request.POST['entity_id'])
    form = EntityForm(request.POST, instance=entity)
    form.save()

, , , .

+5

Source: https://habr.com/ru/post/1746409/


All Articles