Ticket #3055: ticket_30500.patch

File ticket_30500.patch, 2.9 KB (added by marcosmoyano, 4 years ago)

patch proposal

  • 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
    2627
    2728    e = ModelErrorCollection(outfile)
    2829
     
    225226                    "which has not been installed" % (f.name, f.rel.through)
    226227                )
    227228
     229            elif isinstance(f, GenericRelation):
     230                if not any([isinstance(vfield, GenericForeignKey) for vfield in f.rel.to._meta.virtual_fields]):
     231                    e.add(opts, "model '%s' must have a GenericForeignKey in order to create a GenericRelation." % f.rel.to.__name__)
     232
    228233            rel_opts = f.rel.to._meta
    229234            rel_name = RelatedObject(f.rel.to, cls, f).get_accessor_name()
    230235            rel_query_name = f.related_query_name()
  • 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"""
Back to Top