#33004: Inconsistent / Unexpected handling of assigning unsaved model to Generic
Foreign Key
-------------------------------------+-------------------------------------
     Reporter:  Finn Andersen        |                    Owner:  Jonny
                                     |  Park
         Type:  Bug                  |                   Status:  assigned
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  fk, gfk, generic     |             Triage Stage:  Accepted
  foreign key, validation            |
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by wtzhu13):

 The code I defined is as follows. I have verified that the data already
 exists in the database after I save the object of Grade, but I can not use
 it to save the object of Stu.

 class Grade(models.Model):
     id = models.IntegerField(primary_key=True)
     grade = models.CharField(max_length=50, unique=True)


 class Stu(models.Model):
     id = models.IntegerField(primary_key=True)
     name = models.CharField(max_length=50, unique=True)
     grade_of_stu = models.ForeignKey(Grade, to_field="grade",
 on_delete=models.CASCADE)


 ###########################################Django
 
shell##############################################################################
 In [17]: g = Grade(grade="3")

 In [18]: g.save()

 In [19]: stu = Stu(name="Fred", grade_of_stu=g)

 In [20]: stu.save()
 ---------------------------------------------------------------------------
 ValueError                                Traceback (most recent call
 last)
 <ipython-input-20-a0e3017ded06> in <module>
 ----> 1 stu.save()

 C:\ProgramData\Anaconda3\lib\site-packages\django\db\models\base.py in
 save(self, force_insert, force_update, using, update_fields)
     680         non-SQL backends), respectively. Normally, they should not
 be set.
     681         """
 --> 682
 self._prepare_related_fields_for_save(operation_name='save')
     683
     684         using = using or router.db_for_write(self.__class__,
 instance=self)

 C:\ProgramData\Anaconda3\lib\site-packages\django\db\models\base.py in
 _prepare_related_fields_for_save(self, operation_name)
     930                     if not field.remote_field.multiple:
     931
 field.remote_field.delete_cached_value(obj)
 --> 932                     raise ValueError(
     933                         "%s() prohibited to prevent data loss due
 to unsaved "
     934                         "related object '%s'." % (operation_name,
 field.name)

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

 In [21]: stu = Stu(name="Fred", grade_of_stu=Grade.objects.get(grade="3"))

 In [22]: stu.save()

 In [23]:

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33004#comment:8>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/068.328325c7f00ed2eff196ac30fd4fe05e%40djangoproject.com.

Reply via email to