1

I have a database (and model) situation like shown in the following schema:

Database diagram

this is the ModelSerialize:

class PostCreateSerializer(serializers.ModelSerializer):
    base64image = serializers.CharField(allow_blank=True, required=False)
    categories = serializers.ListField(child=serializers.IntegerField(), required=False)

    class Meta:
        model = Post
        fields = ('title', 'text', 'base64image', 'categories')

    def create(self, validated_data):
       cats = None
       if 'categories' in validated_data:
           cats = validated_data.pop('categories')

       post = Post.objects.create(**validated_data)

       if cats is not None:
           for cat in cats:
               PostCategory.objects.create(postid=post, categoryid=Category.objects.get(id=cat), isprimary=False)

       return post

I'm sending this json to the api method

{
"title": "Post Test",
"text": "Body post Test",
"categories": [2,4]
}

but i got this error:

ValueError: save() prohibited to prevent data loss due to unsaved related object 'postid'.

Internal Server Error: /api/post/create/
Traceback (most recent call last):
  File "/Users/alfdev/Lavori/PushApp/Rumorize/rvenv/lib/python3.4/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/alfdev/Lavori/PushApp/Rumorize/rvenv/lib/python3.4/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/alfdev/Lavori/PushApp/Rumorize/rvenv/lib/python3.4/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/alfdev/Lavori/PushApp/Rumorize/rvenv/lib/python3.4/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/alfdev/Lavori/PushApp/Rumorize/rvenv/lib/python3.4/site-packages/rest_framework/views.py", line 466, in dispatch
    response = self.handle_exception(exc)
  File "/Users/alfdev/Lavori/PushApp/Rumorize/rvenv/lib/python3.4/site-packages/rest_framework/views.py", line 463, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/alfdev/Lavori/PushApp/Rumorize/rvenv/lib/python3.4/site-packages/rest_framework/generics.py", line 192, in post
    return self.create(request, *args, **kwargs)
  File "/Users/alfdev/Lavori/PushApp/Rumorize/rvenv/lib/python3.4/site-packages/rest_framework/mixins.py", line 21, in create
    self.perform_create(serializer)
  File "/Users/alfdev/Lavori/PushApp/Rumorize/server/rumorize/contents/views.py", line 117, in perform_create
    PostCategory.objects.create(postid=x, categoryid=Category.objects.get(id=cat), isprimary=False)
  File "/Users/alfdev/Lavori/PushApp/Rumorize/rvenv/lib/python3.4/site-packages/django/db/models/manager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/alfdev/Lavori/PushApp/Rumorize/rvenv/lib/python3.4/site-packages/django/db/models/query.py", line 401, in create
    obj.save(force_insert=True, using=self.db)
  File "/Users/alfdev/Lavori/PushApp/Rumorize/rvenv/lib/python3.4/site-packages/django/db/models/base.py", line 651, in save
    "unsaved related object '%s'." % field.name
ValueError: save() prohibited to prevent data loss due to unsaved related object 'postid'.

My post_create implementation in view:

def perform_create(self, serializer):
# build and save the image
if 'base64image' in serializer.validated_data:
    base64_string = serializer.validated_data.pop('base64image')
    import base64
    import os
    import uuid

    base64_format, base64_string = base64_string.split(';base64,')
    base64_content = base64.b64decode(base64_string)
    filename = '{}{}'.format(uuid.uuid4(), utilities.file_extension_from_base64_format(base64_format))
    path = os.path.join(MEDIA_ROOT, 'post_photos')

    if not os.path.exists(path):
        os.makedirs(path)

    with open(os.path.join(path, filename), 'wb+') as f:
        f.write(base64_content)

    serializer.validated_data['image'] = filename

serializer.save(userid=self.request.user, updateuserid=self.request.user, state=1)

UPDATE

now i got this error: django.db.models.fields.related_descriptors.RelatedObjectDoesNotExist: Post has no id.

these are my models:

class Post(models.Model):
id = models.BigIntegerField(db_column='ID', primary_key=True, editable=False)
title = models.CharField(db_column='Title', max_length=500)
text = models.TextField(db_column='Text')
createdate = models.DateTimeField(db_column='CreateDate', editable=False, default=datetime.datetime.now)
updatedate = models.DateTimeField(db_column='UpdateDate', blank=True, null=True, editable=False, default=datetime.datetime.now)
updateuserid = models.ForeignKey(User, models.DO_NOTHING, db_column='UpdateUserID', blank=True, null=True, related_name='PostUpdateUser')
userid = models.OneToOneField(User, models.DO_NOTHING, db_column='UserID', related_name='creation_user')
image = models.CharField(db_column='Image', max_length=500, blank=True, null=True)
state = models.SmallIntegerField(db_column='State')

class Meta:
    managed = False
    db_table = 'post'
    verbose_name = 'Post'
    verbose_name_plural = 'Posts'

class PostCategory(models.Model): id = models.BigIntegerField(db_column='ID', primary_key=True) # Field name made lowercase. postid = models.ForeignKey(Post, models.DO_NOTHING, db_column='PostID', related_name='posts_categories') # Field name made lowercase. categoryid = models.ForeignKey(Category, models.DO_NOTHING, db_column='CategoryID') # Field name made lowercase. isprimary = models.BooleanField(db_column='IsPrimary') # Field name made lowercase. This field type is a guess.

class Meta:
    managed = False
    db_table = 'post_category'

any suggestions? Thanks

5
  • It feels like your code here doesn't match the error. You get the error because the post was saved which contradicts the Post.objects.create.
    – Linovia
    Mar 22, 2016 at 10:50
  • would you explain better what are you mean?
    – alfdev
    Mar 22, 2016 at 11:15
  • if my approach is not the right one, tell me please how i can save Post instance and related Categories?
    – alfdev
    Mar 22, 2016 at 11:48
  • Sorry, the above comment has a typo. What I meant is that you get the error because the post is not saved which contradicts the Post.objects.create line.
    – Linovia
    Mar 22, 2016 at 11:49
  • ok i understand the error, but have you a solution?
    – alfdev
    Mar 22, 2016 at 11:56

1 Answer 1

1

Try to call the create method of ModelSerializer instead of creating the object on your own. Also the if statement is not necessary and for PostCategory you can use the id fields directly.

class PostCreateSerializer(serializers.ModelSerializer):
    base64image = serializers.CharField(allow_blank=True, required=False)
    categories = serializers.ListField(child=serializers.IntegerField(), required=False)

    class Meta:
        model = Post
        fields = ('title', 'text', 'base64image', 'categories')

    def create(self, validated_data):

       # you don't need the if block
       cats = validated_data.pop('categories', None)

       # try calling the create method of ModelSerializer
       post = super(PostCreateSerializer, self).create(validated_data)

       if cats is not None:
           for cat in cats:
               PostCategory.objects.create(postid_id=post.id, categoryid_id=cat, isprimary=False)

       return post
6
  • Do you use transactions?
    – ilse2005
    Mar 22, 2016 at 13:17
  • No, i don't use transactions
    – alfdev
    Mar 22, 2016 at 13:18
  • Strange. Is this really the full code you have? Can you add the full traceback to your question?
    – ilse2005
    Mar 22, 2016 at 13:20
  • Please add your perform_create method. The error seems to be there: /Users/alfdev/Lavori/PushApp/Rumorize/server/rumorize/contents/views.py", line 117
    – ilse2005
    Mar 22, 2016 at 13:32
  • in the view i've forgotten a test that causes the old exception, now i got this error: django.db.models.fields.related_descriptors.RelatedObjectDoesNotExist: Post has no id.
    – alfdev
    Mar 22, 2016 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.