Ticket #9553: django-related_model_fix.patch

File django-related_model_fix.patch, 1.9 KB (added by Oldřich Jedlička, 16 years ago)

Proof of concept plus test

  • Django-1.0/django/db/models/base.py

    diff -ur django-1.0/Django-1.0/django/db/models/base.py django-1.0.patched/Django-1.0/django/db/models/base.py
    old new  
    1111import django.db.models.manager     # Imported to register signal handler.
    1212from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned, FieldError
    1313from django.db.models.fields import AutoField
    14 from django.db.models.fields.related import OneToOneRel, ManyToOneRel, OneToOneField
     14from django.db.models.fields.related import RelatedField, OneToOneRel, ManyToOneRel, OneToOneField
    1515from django.db.models.query import delete_objects, Q, CollectedObjects
    1616from django.db.models.options import Options
    1717from django.db import connection, transaction, DatabaseError
     
    340340
    341341                self.save_base(raw, parent)
    342342                setattr(self, field.attname, self._get_pk_val(parent._meta))
     343            for field in meta.local_fields:
     344                if isinstance(field, RelatedField):
     345                    if getattr(self, field.attname) is None and getattr(self, field.name) is not None:
     346                        setattr(self, field.attname, getattr(self, field.name).pk)
    343347
    344348        non_pks = [f for f in meta.local_fields if not f.primary_key]
    345349
  • Django-1.0/tests/modeltests/m2m_and_m2o/models.py

    diff -ur django-1.0/Django-1.0/tests/modeltests/m2m_and_m2o/models.py django-1.0.patched/Django-1.0/tests/modeltests/m2m_and_m2o/models.py
    old new  
    4343>>> i3.save()
    4444>>> i3.cc.add(r)
    4545
     46>>> f = User(username='fred')
     47>>> i4 = Issue(num=4, client=f)
     48>>> f.save()
     49>>> i4.save()
     50
    4651>>> from django.db.models.query import Q
    4752
    4853>>> Issue.objects.filter(client=r.id)
Back to Top