Ticket #11702: new_patch.patch

File new_patch.patch, 2.7 KB (added by marcosmoyano, 5 years ago)

New patch

  • django/core/management/validation.py

     
    7979                # so skip the next section
    8080                if isinstance(f.rel.to, (str, unicode)):
    8181                    continue
     82                # Let's make sure we have unique=True if to_field is set in a ForeignKey#
     83                if f.rel.field_name:
     84                    to_field = f.rel.field_name or (r.rel.to._meta.pk and f.rel.to._meta.pk.name)
     85                    if to_field:
     86                        try:
     87                            assert f.rel.to._meta.get_field(to_field).unique, \
     88                                   "Field %s under model %s must have a unique=True constraint." % (to_field, f.rel.to.__name__)
     89                        except:
     90                            e.add(opts, "Field '%s' under model '%s' must have a unique=True constraint." % (to_field, f.rel.to.__name__))
    8291
    8392                rel_opts = f.rel.to._meta
    8493                rel_name = RelatedObject(f.rel.to, cls, f).get_accessor_name()
  • tests/modeltests/invalid_models/models.py

     
    182182    """ Model to test for unique ManyToManyFields, which are invalid. """
    183183    unique_people = models.ManyToManyField( Person, unique=True )
    184184
     185class Author(models.Model):
     186    code = models.CharField(max_length=10, db_index=True)
     187    first_name = models.CharField(max_length=30)
     188    last_name = models.CharField(max_length=40)
    185189
     190class Book(models.Model):
     191    """This model should fail"""
     192    title = models.CharField(max_length=100)
     193    author = models.ForeignKey(Author, to_field='code')
     194
    186195model_errors = """invalid_models.fielderrors: "charfield": CharFields require a "max_length" attribute.
    187196invalid_models.fielderrors: "decimalfield": DecimalFields require a "decimal_places" attribute.
    188197invalid_models.fielderrors: "decimalfield": DecimalFields require a "max_digits" attribute.
     
    279288invalid_models.abstractrelationmodel: 'fk1' has a relation with model AbstractModel, which has either not been installed or is abstract.
    280289invalid_models.abstractrelationmodel: 'fk2' has an m2m relation with model AbstractModel, which has either not been installed or is abstract.
    281290invalid_models.uniquem2m: ManyToManyFields cannot be unique.  Remove the unique argument on 'unique_people'.
     291invalid_models.book: Field 'code' under model 'Author' must have a unique=True constraint.
    282292"""
Back to Top