Chuck Aguilar
Chuck Aguilar

Reputation: 2048

Violation not-null constraint. With a ForeighKey (not user) while trying to post in Django RestFramework

This problem is commented in the whole Internet, but I don't know why, it's almost always with the user.

In my case, the problem is not with the User.

I have this model:

class DocumentDetail(models.Model):
    date = models.DateTimeField(_('date'), auto_now_add=True)
    note = models.TextField(_('note'), blank=True)
    checked = models.BooleanField(_('checked'), default=False)
    details_sample = models.ForeignKey(DocumentDetailSample, related_name='details_sample',
                                       verbose_name=_("details_sample"), blank=False)
    user_id = models.ForeignKey(User, related_name='document_user_id', verbose_name=_('document_user_id'), blank=False)

    class Meta:
        ordering = ('date',)

Where I can user the DocumentDetailSample fields.

The problem is that with the user, I can do this:

def perform_create(self, serializer):
    serializer.save(user_id=self.request.user)

def perform_update(self, serializer):
    serializer.save(user_id=self.request.user)

But with details_sample it should be something passed in the post, because I can't know which "details_sample" it should be.

That's why I thought I could pass the id in the post body.

{
    "note" : "Oh, nice",
    "checked": "True",
    "details_sample": "1"
}

But I get this error:

django.db.utils.IntegrityError: null value in column "details_sample_id" violates not-null constraint DETAIL: Failing row contains (9, 2017-01-24 14:43:58.75642+00, Oh, nice, t, 2, null).

So everything but the details_sample worked. :(

And here's the row:

"1";"2017-01-24 14:40:37.881802+01";"Life is beautiful";"''";"1";"The life is beautiful. And there's also a movie";"This is about life";"{"name": "Chuck", "phone": "123"}"

Does anyone know what could I do?

EDIT

here's my serializer:

class DocumentDetailSerializer(serializers.ModelSerializer):
    title = serializers.ReadOnlyField(source='details_sample.title')
    icon = serializers.ReadOnlyField(source='details_sample.icon')
    priority = serializers.ReadOnlyField(source='details_sample.priority')
    description = serializers.ReadOnlyField(source='details_sample.description')
    prefix = serializers.ReadOnlyField(source='details_sample.prefix')
    advertise = serializers.ReadOnlyField(source='details_sample.advertise')

    class Meta:
        model = DocumentDetail
        fields = (
            'url', 'id', 'date', 'icon', 'checked', 'priority', 'title', 'note', 'description', 'prefix', 'advertise'
        )

I'm able to do it in the admin site, but there I can "use" the whole instance. The problem is while POST. GET works also nicely.

Upvotes: 0

Views: 327

Answers (2)

Linovia
Linovia

Reputation: 20976

You don't have any details_sample field in your serializer so it is simply discarded from the data.

Upvotes: 1

Chuck Aguilar
Chuck Aguilar

Reputation: 2048

I solved it writing this in the ViewSet

    def perform_create(self, serializer):
    serializer.save(
        details_sample=DocumentDetailSample.objects.filter(Q(id=self.request.data['details_sample']))[0],
        user_id=self.request.user)

It's not beautiful, but it works. If someone knows a better way, please tell me.

Upvotes: 1

Related Questions