On Thu, Nov 1, 2012 at 7:10 PM, msoulier <[email protected]> wrote:
> On Nov 1, 1:27 pm, Tom Evans <[email protected]> wrote:
>> >> Please show the definition of MyForm.
>>
>> Please do show it.
>
> Sorry, the model was in the previous email, here's the form.
>
>
> class McdLoadForm(forms.ModelForm):
>     VERSIONPAT = re.compile(r'^(\d+\.){3}\d+$')
>
>     class Meta:
>         model = McdLoad
>         fields = ('version', 'mcdload', 'fromversions')
>
>     def clean(self):
>         cleaned_data = super(McdLoadForm, self).clean()
>         version = cleaned_data.get('version', '')
>         log.debug("cleaned_data: version is %s" % version)
>         if version:
>             version = re.sub(r'^\s+|\s+$', '', version)
>             if not McdLoadForm.VERSIONPAT.match(version):
>                 raise forms.ValidationError, \
>                     "Bad version, should be 4-digits separated by
> commas"
>             cleaned_data['version'] = version
>
>         fromversions = self.cleaned_data.get('fromversions', '')
>         if fromversions:
>             fromversions = re.sub(r'\s', '', fromversions)
>             for version in fromversions.split(','):
>                 if not McdLoadForm.VERSIONPAT.match(version):
>                     raise forms.ValidationError, \
>                         "Bad version, should be 4-digits separated by
> commas"
>             cleaned_data['fromversions'] = fromversions
>
>         log.debug("clean: returning %s" % cleaned_data)
>         return cleaned_data
>

Hmm, looks ok.. do you end up with two entries in the DB, one with the
old primary key, and one with the new primary key?

Personally, I don't use natural primary keys with Django, I always let
it create an 'id' primary key, and then add a unique key to cover the
natural key, which avoids issues when you need to change the natural
key. The logic in ModelForm looks like this:

Update fields in the model with fields from the form
Save the instance:
   Does the instance have a primary key?
     Does that primary key exist in the database?
       Persist changes with UPDATE query
     Else
       Create new entry with INSERT query

If you do this yourself in the shell, you will see that you end up
with two objects. Eg, for this model (chosen as it is a simply model,
no constraints other than primary key):

class PropertyValue(models.Model):
   user = models.ForeignKey('User')
   value = models.CharField(max_length=256)

and then create an instance of it, change the primary key, and save
it, the value is duplicated, not updated.

>>> PropertyValue.objects.create(user=u, value='hello')
<PropertyValue: hello>
>>> PropertyValue.objects.filter(user=u, value='hello').count()
1
>>> instance=PropertyValue.objects.filter(user=u, value='hello').get()
>>> instance.id
67L
>>> instance.id = 1234567
>>> instance.save()
>>> PropertyValue.objects.filter(user=u, value='hello').count()
2


Hope that helps explain what is happening.

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to