Code

Ticket #3055: ticket_30500_2.patch

File ticket_30500_2.patch, 3.4 KB (added by marcosmoyano, 4 years ago)
  • django/core/management/validation.py

     
    2323    from django.db.models.loading import get_app_errors 
    2424    from django.db.models.fields.related import RelatedObject 
    2525    from django.db.models.deletion import SET_NULL, SET_DEFAULT 
     26    from django.contrib.contenttypes.generic import GenericForeignKey, GenericRelation 
     27    try: 
     28        any 
     29    except NameError: 
     30        from django.utils.itercompat import any 
    2631 
    2732    e = ModelErrorCollection(outfile) 
    2833 
     
    225230                    "which has not been installed" % (f.name, f.rel.through) 
    226231                ) 
    227232 
     233            elif isinstance(f, GenericRelation): 
     234                if not any([isinstance(vfield, GenericForeignKey) for vfield in f.rel.to._meta.virtual_fields]): 
     235                    e.add(opts, "model '%s' must have a GenericForeignKey in order to create a GenericRelation." % f.rel.to.__name__) 
     236 
    228237            rel_opts = f.rel.to._meta 
    229238            rel_name = RelatedObject(f.rel.to, cls, f).get_accessor_name() 
    230239            rel_query_name = f.related_query_name() 
  • django/utils/itercompat.py

     
    3737        if not item: 
    3838            return False 
    3939    return True 
     40 
     41def any(iterable): 
     42    for item in iterable: 
     43        if item: 
     44            return True 
     45    return False 
  • tests/modeltests/invalid_models/models.py

     
    55""" 
    66 
    77from django.db import models 
     8from django.contrib.contenttypes import generic 
    89 
    910class FieldErrors(models.Model): 
    1011    charfield = models.CharField() 
     
    216217class InvalidSetDefault(models.Model): 
    217218    fk = models.ForeignKey('self', on_delete=models.SET_DEFAULT) 
    218219 
     220class Tag(models.Model): 
     221   name = models.CharField("name", max_length=20) 
    219222 
     223class TaggedObject(models.Model): 
     224   object_id = models.PositiveIntegerField("Object ID") 
     225   tag = models.ForeignKey(Tag) 
     226   content_object = generic.GenericForeignKey() 
     227 
     228class UserTaggedObject(models.Model): 
     229   object_tag = models.ForeignKey(TaggedObject) 
     230 
     231class ArticleAttachment(models.Model): 
     232   tags = generic.GenericRelation(TaggedObject) 
     233   user_tags = generic.GenericRelation(UserTaggedObject) 
     234 
    220235model_errors = """invalid_models.fielderrors: "charfield": CharFields require a "max_length" attribute that is a positive integer. 
    221236invalid_models.fielderrors: "charfield2": CharFields require a "max_length" attribute that is a positive integer. 
    222237invalid_models.fielderrors: "charfield3": CharFields require a "max_length" attribute that is a positive integer. 
     
    324339invalid_models.nonexistingorderingwithsingleunderscore: "ordering" refers to "does_not_exist", a field that doesn't exist. 
    325340invalid_models.invalidsetnull: 'fk' specifies on_delete=SET_NULL, but cannot be null. 
    326341invalid_models.invalidsetdefault: 'fk' specifies on_delete=SET_DEFAULT, but has no default value. 
     342invalid_models.articleattachment: model 'UserTaggedObject' must have a GenericForeignKey in order to create a GenericRelation. 
    327343"""