0

I have a model with 'created' field. I want store date only when new object is created, but do not update this field when I change some other fields in model. Before sync i had auto_now but I changed it like in first answer Automatic creation date for django model form objects?. My problem is when I update my entity

IntegrityError at /rhyme/15/
null value in column "created" violates not-null constraint
DETAIL:  Failing row contains (15, LORE, <p>LORE</p>
<p>LORE</p>
<p>LORE</p>
<p>LORE</p>
<p>LORE</p>
<p>L..., null, 1, 1).

My model:

class Rhyme(models.Model):
    title = models.CharField(max_length=255)
    content = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey(UserProfile, related_name='created_rhymes')
    profiles = models.ManyToManyField(UserProfile, related_name='stored_rhymes')
    category = models.ForeignKey(Category, null=True, blank=True, related_name='rhymes')

update view:

form = RhymeForm(data)
form_is_valid = form.is_valid()
if form_is_valid:
    rhyme = form.save(commit=False)
    if self.kwargs.has_key('id'):
        rhyme.id = self.kwargs['id']
    rhyme.author = self.request.user.profile
    rhyme.save()

form:

class RhymeForm(forms.ModelForm):
    title = forms.CharField(
        widget=forms.TextInput(attrs={'class':'form-control'}),
        label=u'Tytuł'
    )
    content = forms.CharField(
        widget=forms.Textarea(attrs={'class':'form-control'}),
        label=u'Treść'
    )
    category = forms.ModelChoiceField(
        queryset=Category.objects.all(),
        label=u'Kategoria',
        empty_label="     ",
        required=False
    )
    class Meta:
        model = Rhyme
        fields = ('title', 'content', 'category')
    def save(self, commit=True):
        rhyme = super(RhymeForm, self).save(commit=False)
        if commit is True:
            rhyme.save()
        return rhyme

template (bit weird but json sends just category, content and title):

<form action="{$ formAction $}" role="form" method="post" ng-submit="add($event)">
            {% for field in form.visible_fields %}
                <div class="form-group {% if field.errors %}has-error{% endif %}">
                    {{ field.label_tag }}
                    {{ field|ngmodel }}
                    {% with 'errors.'|add:field.name as error_field %}
                        <div class="text-warning bg bg-danger">
                            {{ field.errors }}{$ {{ error_field }} $}
                        </div>
                    {% endwith %}
                </div>
            {% endfor %}
            {% csrf_token %}
            <div class="row">
                <button ng-click="close($event)" class="btn btn-default col-md-3">Anuluj</button>
                <input class="btn btn-success col-md-offset-1 col-md-8" type="submit" value="Zapisz" />
            </div>
        </form>

My ugly solution is to put

rhyme.created = models.Rhyme.objects.get(pk=self.kwargs['id']).created

But why my ORM trying to put null to field created, how to solve this?

Community
  • 1
  • 1
tstr
  • 1,254
  • 20
  • 35
  • How are you updating it? – Rohan Jul 25 '14 at 10:39
  • Could you please display a snippet of your template file and the related view which raise this error ? – rphonika Jul 25 '14 at 11:02
  • I thought the general way to have a 'created' field is set the default value to 'lambda:datetime.datetime.now()'. I somehow remember that there is some down side about using 'auto_now_add'. – Jerry Meng Jul 25 '14 at 13:40

0 Answers0