Django

Code

Changeset 8488

Show
Ignore:
Timestamp:
08/23/08 11:56:41 (3 months ago)
Author:
mtredinnick
Message:

Fixed #7042 -- The management validation command nows alerts users to the
presence (and incorrectness) of unique=True on ManyToManyFields?. This has never
worked and generates invalid SQL. Now it's raised as an explicit error during
validation. Thanks to clamothe for the patch.

Still needs a docs change to make this clear, but that goes to the docs
refactor branch.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/management/validation.py

    r8442 r8488  
    100100                            e.add(opts, "Reverse query name for field '%s' clashes with related field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.get_accessor_name(), f.name)) 
    101101 
    102         seen_intermediary_signatures = []  
     102        seen_intermediary_signatures = [] 
    103103        for i, f in enumerate(opts.local_many_to_many): 
    104104            # Check to see if the related m2m field will clash with any 
     
    111111                if isinstance(f.rel.to, (str, unicode)): 
    112112                    continue 
     113 
     114            # Check that the field is not set to unique.  ManyToManyFields do not support unique. 
     115            if f.unique: 
     116                e.add(opts, "ManyToManyFields cannot be unique.  Remove the unique argument on '%s'." % f.name) 
     117 
    113118            if getattr(f.rel, 'through', None) is not None: 
    114119                if hasattr(f.rel, 'through_model'): 
     
    153158                else: 
    154159                    e.add(opts, "'%s' specifies an m2m relation through model %s, which has not been installed" % (f.name, f.rel.through)) 
    155              
     160 
    156161            rel_opts = f.rel.to._meta 
    157162            rel_name = RelatedObject(f.rel.to, cls, f).get_accessor_name() 
  • django/trunk/tests/modeltests/invalid_models/models.py

    r8442 r8488  
    111111    rel1 = models.ForeignKey("Rel1") 
    112112    rel2 = models.ManyToManyField("Rel2") 
    113      
     113 
    114114class MissingManualM2MModel(models.Model): 
    115115    name = models.CharField(max_length=5) 
    116116    missing_m2m = models.ManyToManyField(Model, through="MissingM2MModel") 
    117      
     117 
    118118class Person(models.Model): 
    119119    name = models.CharField(max_length=5) 
     
    177177    fk1 = models.ForeignKey('AbstractModel') 
    178178    fk2 = models.ManyToManyField('AbstractModel') 
    179      
     179 
     180class UniqueM2M(models.Model): 
     181    """ Model to test for unique ManyToManyFields, which are invalid. """ 
     182    unique_people = models.ManyToManyField( Person, unique=True ) 
     183 
    180184model_errors = """invalid_models.fielderrors: "charfield": CharFields require a "max_length" attribute. 
    181185invalid_models.fielderrors: "decimalfield": DecimalFields require a "decimal_places" attribute. 
     
    272276invalid_models.abstractrelationmodel: 'fk1' has a relation with model AbstractModel, which has either not been installed or is abstract. 
    273277invalid_models.abstractrelationmodel: 'fk2' has an m2m relation with model AbstractModel, which has either not been installed or is abstract. 
     278invalid_models.uniquem2m: ManyToManyFields cannot be unique.  Remove the unique argument on 'unique_people'. 
    274279"""