Django

Code

Ticket #3323: django-string-rel-with-test.patch

File django-string-rel-with-test.patch, 4.7 kB (added by shaleh, 7 months ago)

update to the recent patch, adds tests keeps the behavior consistent

  • django/db/models/options.py

    old new  
    152152            rel_objs = [] 
    153153            for klass in get_models(): 
    154154                for f in klass._meta.fields: 
    155                     if f.rel and self == f.rel.to._meta: 
     155                    if f.rel and not isinstance(f.rel.to, str) and self == f.rel.to._meta: 
    156156                        rel_objs.append(RelatedObject(f.rel.to, klass, f)) 
    157157            self._all_related_objects = rel_objs 
    158158            return rel_objs 
     
    186186            rel_objs = [] 
    187187            for klass in get_models(): 
    188188                for f in klass._meta.many_to_many: 
    189                     if f.rel and self == f.rel.to._meta: 
     189                    if f.rel and not isinstance(f.rel.to, str) and self == f.rel.to._meta: 
    190190                        rel_objs.append(RelatedObject(f.rel.to, klass, f)) 
    191191            if app_cache_ready(): 
    192192                self._all_related_many_to_many_objects = rel_objs 
  • django/core/management/validation.py

    old new  
    7272            # Check to see if the related field will clash with any 
    7373            # existing fields, m2m fields, m2m related objects or related objects 
    7474            if f.rel: 
    75                 rel_opts = f.rel.to._meta 
    7675                if f.rel.to not in models.get_models(): 
    77                     e.add(opts, "'%s' has relation with model %s, which has not been installed" % (f.name, rel_opts.object_name)) 
     76                    e.add(opts, "'%s' has relation with model %s, which has not been installed" % (f.name, f.rel.to)) 
     77                # it is a string and we could not find the model it refers to 
     78                # so skip the next section 
     79                if isinstance(f.rel.to, (str, unicode)): 
     80                    continue 
    7881 
     82                rel_opts = f.rel.to._meta 
    7983                rel_name = RelatedObject(f.rel.to, cls, f).get_accessor_name() 
    8084                rel_query_name = f.related_query_name() 
    8185                for r in rel_opts.fields: 
     
    103104        for i, f in enumerate(opts.many_to_many): 
    104105            # Check to see if the related m2m field will clash with any 
    105106            # existing fields, m2m fields, m2m related objects or related objects 
    106             rel_opts = f.rel.to._meta 
    107107            if f.rel.to not in models.get_models(): 
    108                 e.add(opts, "'%s' has m2m relation with model %s, which has not been installed" % (f.name, rel_opts.object_name)) 
     108                e.add(opts, "'%s' has m2m relation with model %s, which has not been installed" % (f.name, f.rel.to)) 
     109                # it is a string and we could not find the model it refers to 
     110                # so skip the next section 
     111                if isinstance(f.rel.to, (str, unicode)): 
     112                    continue 
    109113 
     114            rel_opts = f.rel.to._meta 
    110115            rel_name = RelatedObject(f.rel.to, cls, f).get_accessor_name() 
    111116            rel_query_name = f.related_query_name() 
    112117            # If rel_name is none, there is no reverse accessor. 
  • tests/modeltests/invalid_models/models.py

    old new  
    108108    colour = models.CharField(max_length=5) 
    109109    model = models.ForeignKey(Model) 
    110110 
     111class MissingRelations(models.Model): 
     112    rel1 = models.ForeignKey("Rel1") 
     113    rel2 = models.ManyToManyField("Rel2") 
     114 
    111115model_errors = """invalid_models.fielderrors: "charfield": CharFields require a "max_length" attribute. 
    112116invalid_models.fielderrors: "decimalfield": DecimalFields require a "decimal_places" attribute. 
    113117invalid_models.fielderrors: "decimalfield": DecimalFields require a "max_digits" attribute. 
     
    191195invalid_models.selfclashm2m: Accessor for m2m field 'm2m_4' clashes with related m2m field 'SelfClashM2M.selfclashm2m_set'. Add a related_name argument to the definition for 'm2m_4'. 
    192196invalid_models.selfclashm2m: Reverse query name for m2m field 'm2m_3' clashes with field 'SelfClashM2M.selfclashm2m'. Add a related_name argument to the definition for 'm2m_3'. 
    193197invalid_models.selfclashm2m: Reverse query name for m2m field 'm2m_4' clashes with field 'SelfClashM2M.selfclashm2m'. Add a related_name argument to the definition for 'm2m_4'. 
     198invalid_models.missingrelations: 'rel2' has m2m relation with model Rel2, which has not been installed 
     199invalid_models.missingrelations: 'rel1' has relation with model Rel1, which has not been installed 
    194200"""