Django

Code

Changeset 549

Show
Ignore:
Timestamp:
08/25/05 17:51:30 (3 years ago)
Author:
adrian
Message:

Fixed #122 -- BIG, BACKWARDS-INCOMPATIBLE CHANGE. Changed model syntax to use fieldname=FieldClass?() syntax. See ModelSyntaxChangeInstructions for important information on how to change your models

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • djangoproject.com/django_website/apps/aggregator/models/aggregator.py

    r428 r549  
    22 
    33class Feed(meta.Model): 
    4     fields = ( 
    5         meta.CharField('title', maxlength=200), 
    6         meta.URLField('feed_url', unique=True), 
    7         meta.URLField('public_url'), 
    8         meta.BooleanField("is_defunct"), 
    9     ) 
    10     admin = meta.Admin() 
     4    title = meta.CharField(maxlength=200) 
     5    feed_url = meta.URLField(unique=True) 
     6    public_url = meta.URLField() 
     7    is_defunct = meta.BooleanField() 
     8    class META: 
     9        admin = meta.Admin() 
    1110 
    1211    def __repr__(self): 
     
    1413 
    1514class FeedItem(meta.Model): 
    16     fields = ( 
    17         meta.ForeignKey(Feed), 
    18         meta.CharField('title', maxlength=200), 
    19         meta.URLField('link'), 
    20         meta.TextField('summary', blank=True), 
    21         meta.DateTimeField('date_modified'), 
    22         meta.CharField('guid', maxlength=200, unique=True, db_index=True), 
    23     ) 
    24     ordering = ("-date_modified",) 
     15    feed = meta.ForeignKey(Feed) 
     16    title = meta.CharField(maxlength=200) 
     17    link = meta.URLField() 
     18    summary = meta.TextField(blank=True) 
     19    date_modified = meta.DateTimeField() 
     20    guid = meta.CharField(maxlength=200, unique=True, db_index=True) 
     21    class META: 
     22        ordering = ("-date_modified",) 
    2523 
    2624    def __repr__(self): 
    2725        return self.title 
    28          
     26 
    2927    def get_absolute_url(self): 
    3028        return self.link 
  • djangoproject.com/django_website/apps/blog/models/blog.py

    r46 r549  
    22 
    33class Entry(meta.Model): 
    4     verbose_name_plural = 'entries' 
    5     module_name = 'entries' 
    6     fields = ( 
    7         meta.DateTimeField('pub_date', 'publication date'), 
    8         meta.SlugField('slug', 'slug', unique_for_date='pub_date'), 
    9         meta.CharField('headline', 'headline', maxlength=200), 
    10         meta.TextField('summary', 'summary', help_text="Use raw HTML."), 
    11         meta.TextField('body', 'body', help_text="Use raw HTML."), 
    12         meta.CharField('author', 'author', maxlength=100), 
    13     ) 
    14     ordering = (('pub_date', 'DESC'),) 
    15     get_latest_by = 'pub_date' 
    16     admin = meta.Admin( 
    17         fields = ( 
    18             (None, {'fields': ('pub_date', 'slug', 'author', 'headline', 'summary', 'body')}), 
    19         ), 
    20         list_display = ('pub_date', 'headline', 'author'), 
    21     ) 
     4    pub_date = meta.DateTimeField(), 
     5    slug = meta.SlugField(unique_for_date='pub_date') 
     6    headline = meta.CharField(maxlength=200) 
     7    summary = meta.TextField(help_text="Use raw HTML.") 
     8    body = meta.TextField(help_text="Use raw HTML.") 
     9    author = meta.CharField(maxlength=100) 
     10    class META: 
     11        verbose_name_plural = 'entries' 
     12        module_name = 'entries' 
     13        ordering = ('-pub_date',) 
     14        get_latest_by = 'pub_date' 
     15        admin = meta.Admin( 
     16            list_display = ('pub_date', 'headline', 'author'), 
     17        ) 
    2218 
    2319    def __repr__(self): 
  • djangoproject.com/django_website/apps/docs/models/docs.py

    r349 r549  
    22 
    33class Document(meta.Model): 
    4     fields = ( 
    5         meta.CharField('title', maxlength=200), 
    6         meta.CharField('slug', maxlength=50, unique=True, prepopulate_from=('title',)), 
    7         meta.CharField('doc_path', maxlength=200, 
    8             help_text="Relative to the docs directory in django SVN; leave off the file extension"), 
    9         meta.DateTimeField('last_updated', 'last updated', auto_now=True), 
    10     ) 
    11     ordering = ('title',) 
    12     admin = meta.Admin( 
    13         fields = ( 
    14             (None, {'fields': ('title', 'slug', 'doc_path')}), 
    15         ), 
    16         list_display = ('title', 'doc_path'), 
    17     ) 
     4    title = meta.CharField(maxlength=200) 
     5    slug = meta.CharField(maxlength=50, unique=True, prepopulate_from=('title',)) 
     6    doc_path = meta.CharField(maxlength=200, 
     7        help_text="Relative to the docs directory in django SVN. Leave off the file extension.") 
     8    last_updated = meta.DateTimeField(auto_now=True) 
     9    class META: 
     10        ordering = ('title',) 
     11        admin = meta.Admin( 
     12            fields = ( 
     13                (None, {'fields': ('title', 'slug', 'doc_path')}), 
     14            ), 
     15            list_display = ('title', 'doc_path'), 
     16        ) 
    1817 
    1918    def __repr__(self): 
  • django/trunk/django/contrib/comments/models/comments.py

    r354 r549  
    33 
    44class Comment(meta.Model): 
    5     db_table = 'comments' 
    6     fields = ( 
    7         meta.ForeignKey(auth.User, raw_id_admin=True), 
    8         meta.ForeignKey(core.ContentType, name='content_type_id', rel_name='content_type'), 
    9         meta.IntegerField('object_id', 'object ID'), 
    10         meta.CharField('headline', 'headline', maxlength=255, blank=True), 
    11         meta.TextField('comment', 'comment', maxlength=3000), 
    12         meta.PositiveSmallIntegerField('rating1', 'rating #1', blank=True, null=True), 
    13         meta.PositiveSmallIntegerField('rating2', 'rating #2', blank=True, null=True), 
    14         meta.PositiveSmallIntegerField('rating3', 'rating #3', blank=True, null=True), 
    15         meta.PositiveSmallIntegerField('rating4', 'rating #4', blank=True, null=True), 
    16         meta.PositiveSmallIntegerField('rating5', 'rating #5', blank=True, null=True), 
    17         meta.PositiveSmallIntegerField('rating6', 'rating #6', blank=True, null=True), 
    18         meta.PositiveSmallIntegerField('rating7', 'rating #7', blank=True, null=True), 
    19         meta.PositiveSmallIntegerField('rating8', 'rating #8', blank=True, null=True), 
    20         # This field designates whether to use this row's ratings in 
    21         # aggregate functions (summaries). We need this because people are 
    22         # allowed to post multiple review on the same thing, but the system 
    23         # will only use the latest one (with valid_rating=True) in tallying 
    24         # the reviews. 
    25         meta.BooleanField('valid_rating', 'is valid rating'), 
    26         meta.DateTimeField('submit_date', 'date/time submitted', auto_now_add=True), 
    27         meta.BooleanField('is_public', 'is public'), 
    28         meta.IPAddressField('ip_address', 'IP address', blank=True, null=True), 
    29         meta.BooleanField('is_removed', 'is removed', 
    30             help_text='Check this box if the comment is inappropriate. A "This comment has been removed" message will be displayed instead.'), 
    31         meta.ForeignKey(core.Site), 
    32     ) 
    33     module_constants = { 
    34         # min. and max. allowed dimensions for photo resizing (in pixels) 
    35         'MIN_PHOTO_DIMENSION': 5, 
    36         'MAX_PHOTO_DIMENSION': 1000, 
    37  
    38         # option codes for comment-form hidden fields 
    39         'PHOTOS_REQUIRED': 'pr', 
    40         'PHOTOS_OPTIONAL': 'pa', 
    41         'RATINGS_REQUIRED': 'rr', 
    42         'RATINGS_OPTIONAL': 'ra', 
    43         'IS_PUBLIC': 'ip', 
    44     } 
    45     ordering = ('-submit_date',) 
    46     admin = meta.Admin( 
    47         fields = ( 
    48             (None, {'fields': ('content_type_id', 'object_id', 'site_id')}), 
    49             ('Content', {'fields': ('user_id', 'headline', 'comment')}), 
    50             ('Ratings', {'fields': ('rating1', 'rating2', 'rating3', 'rating4', 'rating5', 'rating6', 'rating7', 'rating8', 'valid_rating')}), 
    51             ('Meta', {'fields': ('is_public', 'is_removed', 'ip_address')}), 
    52         ), 
    53         list_display = ('user_id', 'submit_date', 'content_type_id', 'get_content_object'), 
    54         list_filter = ('submit_date',), 
    55         date_hierarchy = 'submit_date', 
    56         search_fields = ('comment', 'user__username'), 
    57     ) 
     5    user = meta.ForeignKey(auth.User, raw_id_admin=True) 
     6    content_type = meta.ForeignKey(core.ContentType) 
     7    object_id = meta.IntegerField('object ID') 
     8    headline = meta.CharField(maxlength=255, blank=True) 
     9    comment = meta.TextField(maxlength=3000) 
     10    rating1 = meta.PositiveSmallIntegerField('rating #1', blank=True, null=True) 
     11    rating2 = meta.PositiveSmallIntegerField('rating #2', blank=True, null=True) 
     12    rating3 = meta.PositiveSmallIntegerField('rating #3', blank=True, null=True) 
     13    rating4 = meta.PositiveSmallIntegerField('rating #4', blank=True, null=True) 
     14    rating5 = meta.PositiveSmallIntegerField('rating #5', blank=True, null=True) 
     15    rating6 = meta.PositiveSmallIntegerField('rating #6', blank=True, null=True) 
     16    rating7 = meta.PositiveSmallIntegerField('rating #7', blank=True, null=True) 
     17    rating8 = meta.PositiveSmallIntegerField('rating #8', blank=True, null=True) 
     18    # This field designates whether to use this row's ratings in aggregate 
     19    # functions (summaries). We need this because people are allowed to post 
     20    # multiple reviews on the same thing, but the system will only use the 
     21    # latest one (with valid_rating=True) in tallying the reviews. 
     22    valid_rating = meta.BooleanField('is valid rating') 
     23    submit_date = meta.DateTimeField('date/time submitted', auto_now_add=True) 
     24    is_public = meta.BooleanField() 
     25    ip_address = meta.IPAddressField('IP address', blank=True, null=True) 
     26    is_removed = meta.BooleanField(help_text='Check this box if the comment is inappropriate. A "This comment has been removed" message will be displayed instead.') 
     27    site = meta.ForeignKey(core.Site) 
     28    class META: 
     29        db_table = 'comments' 
     30        module_constants = { 
     31            # min. and max. allowed dimensions for photo resizing (in pixels) 
     32            'MIN_PHOTO_DIMENSION': 5, 
     33            'MAX_PHOTO_DIMENSION': 1000, 
     34 
     35            # option codes for comment-form hidden fields 
     36            'PHOTOS_REQUIRED': 'pr', 
     37            'PHOTOS_OPTIONAL': 'pa', 
     38            'RATINGS_REQUIRED': 'rr', 
     39            'RATINGS_OPTIONAL': 'ra', 
     40            'IS_PUBLIC': 'ip', 
     41        } 
     42        ordering = ('-submit_date',) 
     43        admin = meta.Admin( 
     44            fields = ( 
     45                (None, {'fields': ('content_type', 'object_id', 'site')}), 
     46                ('Content', {'fields': ('user', 'headline', 'comment')}), 
     47                ('Ratings', {'fields': ('rating1', 'rating2', 'rating3', 'rating4', 'rating5', 'rating6', 'rating7', 'rating8', 'valid_rating')}), 
     48                ('Meta', {'fields': ('is_public', 'is_removed', 'ip_address')}), 
     49            ), 
     50            list_display = ('user', 'submit_date', 'content_type', 'get_content_object'), 
     51            list_filter = ('submit_date',), 
     52            date_hierarchy = 'submit_date', 
     53            search_fields = ('comment', 'user__username'), 
     54        ) 
    5855 
    5956    def __repr__(self): 
     
    157154 
    158155class FreeComment(meta.Model): 
    159     "A FreeComment is a comment by a non-registered user" 
    160     db_table = 'comments_free' 
    161     fields = ( 
    162         meta.ForeignKey(core.ContentType, name='content_type_id', rel_name='content_type'), 
    163         meta.IntegerField('object_id', 'object ID'), 
    164         meta.TextField('comment', 'comment', maxlength=3000), 
    165         meta.CharField('person_name', "person's name", maxlength=50), 
    166         meta.DateTimeField('submit_date', 'date/time submitted', auto_now_add=True), 
    167         meta.BooleanField('is_public', 'is public'), 
    168         meta.IPAddressField('ip_address', 'IP address'), 
    169         # TODO: Change this to is_removed, like Comment 
    170         meta.BooleanField('approved', 'approved by staff'), 
    171         meta.ForeignKey(core.Site), 
    172     ) 
    173     ordering = ('-submit_date',) 
    174     admin = meta.Admin( 
    175         fields = ( 
    176             (None, {'fields': ('content_type_id', 'object_id', 'site_id')}), 
    177             ('Content', {'fields': ('person_name', 'comment')}), 
    178             ('Meta', {'fields': ('submit_date', 'is_public', 'ip_address', 'approved')}), 
    179         ), 
    180         list_display = ('person_name', 'submit_date', 'content_type_id', 'get_content_object'), 
    181         list_filter = ('submit_date',), 
    182         date_hierarchy = 'submit_date', 
    183         search_fields = ('comment', 'person_name'), 
    184     ) 
     156    # A FreeComment is a comment by a non-registered user. 
     157    content_type = meta.ForeignKey(core.ContentType) 
     158    object_id = meta.IntegerField('object ID') 
     159    comment = meta.TextField(maxlength=3000) 
     160    person_name = meta.CharField("person's name", maxlength=50) 
     161    submit_date = meta.DateTimeField('date/time submitted', auto_now_add=True) 
     162    is_public = meta.BooleanField() 
     163    ip_address = meta.IPAddressField() 
     164    # TODO: Change this to is_removed, like Comment 
     165    approved = meta.BooleanField('approved by staff') 
     166    site = meta.ForeignKey(core.Site) 
     167    class META: 
     168        db_table = 'comments_free' 
     169        ordering = ('-submit_date',) 
     170        admin = meta.Admin( 
     171            fields = ( 
     172                (None, {'fields': ('content_type', 'object_id', 'site')}), 
     173                ('Content', {'fields': ('person_name', 'comment')}), 
     174                ('Meta', {'fields': ('submit_date', 'is_public', 'ip_address', 'approved')}), 
     175            ), 
     176            list_display = ('person_name', 'submit_date', 'content_type', 'get_content_object'), 
     177            list_filter = ('submit_date',), 
     178            date_hierarchy = 'submit_date', 
     179            search_fields = ('comment', 'person_name'), 
     180        ) 
    185181 
    186182    def __repr__(self): 
     
    204200 
    205201class KarmaScore(meta.Model): 
    206     module_name = 'karma' 
    207     fields = ( 
    208         meta.ForeignKey(auth.User), 
    209         meta.ForeignKey(Comment), 
    210         meta.SmallIntegerField('score', 'score', db_index=True), 
    211         meta.DateTimeField('scored_date', 'date scored', auto_now=True), 
    212     ) 
    213     unique_together = (('user_id', 'comment_id'),) 
    214     module_constants = { 
    215         # what users get if they don't have any karma 
    216         'DEFAULT_KARMA': 5, 
    217         'KARMA_NEEDED_BEFORE_DISPLAYED': 3, 
    218     } 
     202    user = meta.ForeignKey(auth.User) 
     203    comment = meta.ForeignKey(Comment) 
     204    score = meta.SmallIntegerField(db_index=True) 
     205    scored_date = meta.DateTimeField(auto_now=True) 
     206    class META: 
     207        module_name = 'karma' 
     208        unique_together = (('user', 'comment'),) 
     209        module_constants = { 
     210            # what users get if they don't have any karma 
     211            'DEFAULT_KARMA': 5, 
     212            'KARMA_NEEDED_BEFORE_DISPLAYED': 3, 
     213        } 
    219214 
    220215    def __repr__(self): 
     
    223218    def _module_vote(user_id, comment_id, score): 
    224219        try: 
    225             karma = get_object(comment_id__exact=comment_id, user_id__exact=user_id) 
     220            karma = get_object(comment__id__exact=comment_id, user__id__exact=user_id) 
    226221        except KarmaScoreDoesNotExist: 
    227222            karma = KarmaScore(None, user_id, comment_id, score, datetime.datetime.now()) 
     
    242237 
    243238class UserFlag(meta.Model): 
    244     db_table = 'comments_user_flags' 
    245     fields = ( 
    246         meta.ForeignKey(auth.User), 
    247         meta.ForeignKey(Comment), 
    248         meta.DateTimeField('flag_date', 'date flagged', auto_now_add=True), 
    249     ) 
    250     unique_together = (('user_id', 'comment_id'),) 
     239    user = meta.ForeignKey(auth.User) 
     240    comment = meta.ForeignKey(Comment) 
     241    flag_date = meta.DateTimeField(auto_now_add=True) 
     242    class META: 
     243        db_table = 'comments_user_flags' 
     244        unique_together = (('user', 'comment'),) 
    251245 
    252246    def __repr__(self): 
     
    262256            return # A user can't flag his own comment. Fail silently. 
    263257        try: 
    264             f = get_object(user_id__exact=user.id, comment_id__exact=comment.id) 
     258            f = get_object(user__id__exact=user.id, comment__id__exact=comment.id) 
    265259        except UserFlagDoesNotExist: 
    266260            from django.core.mail import mail_managers 
     
    271265 
    272266class ModeratorDeletion(meta.Model): 
    273     db_table = 'comments_moderator_deletions' 
    274     fields = ( 
    275         meta.ForeignKey(auth.User, verbose_name='moderator'), 
    276         meta.ForeignKey(Comment), 
    277         meta.DateTimeField('deletion_date', 'date deleted', auto_now_add=True), 
    278     ) 
    279     unique_together = (('user_id', 'comment_id'),) 
     267    user = meta.ForeignKey(auth.User, verbose_name='moderator') 
     268    comment = meta.ForeignKey(Comment) 
     269    deletion_date = meta.DateTimeField(auto_now_add=True) 
     270    class META: 
     271        db_table = 'comments_moderator_deletions' 
     272        unique_together = (('user', 'comment'),) 
    280273 
    281274    def __repr__(self): 
  • django/trunk/django/contrib/comments/templatetags/comments.py

    r377 r549  
    124124        comment_count = get_count_function(object_id__exact=self.obj_id, 
    125125            content_type__package__label__exact=self.package, 
    126             content_type__python_module_name__exact=self.module, site_id__exact=SITE_ID) 
     126            content_type__python_module_name__exact=self.module, site__id__exact=SITE_ID) 
    127127        context[self.var_name] = comment_count 
    128128        return '' 
     
    147147            'content_type__package__label__exact': self.package, 
    148148            'content_type__python_module_name__exact': self.module, 
    149             'site_id__exact': SITE_ID, 
     149            'site__id__exact': SITE_ID, 
    150150            'select_related': True, 
    151151            'order_by': (self.ordering + 'submit_date',), 
  • django/trunk/django/contrib/comments/views/comments.py

    r518 r549  
    8787        today = datetime.date.today() 
    8888        c = self.get_comment(new_data) 
    89         for old in comments.get_list(content_type_id__exact=new_data["content_type_id"], 
    90             object_id__exact=new_data["object_id"], user_id__exact=self.get_user_id()): 
     89        for old in comments.get_list(content_type__id__exact=new_data["content_type_id"], 
     90            object_id__exact=new_data["object_id"], user__id__exact=self.get_user_id()): 
    9191            # Check that this comment isn't duplicate. (Sometimes people post 
    9292            # comments twice by mistake.) If it is, fail silently by pretending 
     
    142142        # comments twice by mistake.) If it is, fail silently by pretending 
    143143        # the comment was posted successfully. 
    144         for old_comment in freecomments.get_list(content_type_id__exact=new_data["content_type_id"], 
     144        for old_comment in freecomments.get_list(content_type__id__exact=new_data["content_type_id"], 
    145145            object_id__exact=new_data["object_id"], person_name__exact=new_data["person_name"], 
    146146            submit_date__year=today.year, submit_date__month=today.month, 
  • django/trunk/django/contrib/comments/views/userflags.py

    r316 r549  
    1717    """ 
    1818    try: 
    19         comment = comments.get_object(pk=comment_id, site_id__exact=SITE_ID) 
     19        comment = comments.get_object(pk=comment_id, site__id__exact=SITE_ID) 
    2020    except comments.CommentDoesNotExist: 
    2121        raise Http404 
     
    3232def flag_done(request, comment_id): 
    3333    try: 
    34         comment = comments.get_object(pk=comment_id, site_id__exact=SITE_ID) 
     34        comment = comments.get_object(pk=comment_id, site__id__exact=SITE_ID) 
    3535    except comments.CommentDoesNotExist: 
    3636        raise Http404 
     
    5151    """ 
    5252    try: 
    53         comment = comments.get_object(pk=comment_id, site_id__exact=SITE_ID) 
     53        comment = comments.get_object(pk=comment_id, site__id__exact=SITE_ID) 
    5454    except comments.CommentDoesNotExist: 
    5555        raise Http404 
     
    7373def delete_done(request, comment_id): 
    7474    try: 
    75         comment = comments.get_object(pk=comment_id, site_id__exact=SITE_ID) 
     75        comment = comments.get_object(pk=comment_id, site__id__exact=SITE_ID) 
    7676    except comments.CommentDoesNotExist: 
    7777        raise Http404 
  • django/trunk/django/core/management.py

    r539 r549  
    6868            col_type = db.DATA_TYPES[data_type] 
    6969            if col_type is not None: 
    70                 field_output = [f.name, col_type % rel_field.__dict__] 
     70                field_output = [f.column, col_type % rel_field.__dict__] 
    7171                field_output.append('%sNULL' % (not f.null and 'NOT ' or '')) 
    7272                if f.unique: 
     
    7676                if f.rel: 
    7777                    field_output.append('REFERENCES %s (%s)' % \ 
    78                         (f.rel.to.db_table, f.rel.to.get_field(f.rel.field_name).name)) 
     78                        (f.rel.to.db_table, f.rel.to.get_field(f.rel.field_name).column)) 
    7979                table_output.append(' '.join(field_output)) 
    8080        if opts.order_with_respect_to: 
    8181            table_output.append('_order %s NULL' % db.DATA_TYPES['IntegerField']) 
    8282        for field_constraints in opts.unique_together: 
    83             table_output.append('UNIQUE (%s)' % ", ".join(field_constraints)) 
     83            table_output.append('UNIQUE (%s)' % ", ".join([opts.get_field(f).column for f in field_constraints])) 
    8484 
    8585        full_statement = ['CREATE TABLE %s (' % opts.db_table] 
     
    9595            table_output.append('    id %s NOT NULL PRIMARY KEY,' % db.DATA_TYPES['AutoField']) 
    9696            table_output.append('    %s_id %s NOT NULL REFERENCES %s (%s),' % \ 
    97                 (opts.object_name.lower(), db.DATA_TYPES['IntegerField'], opts.db_table, opts.pk.name)) 
     97                (opts.object_name.lower(), db.DATA_TYPES['IntegerField'], opts.db_table, opts.pk.column)) 
    9898            table_output.append('    %s_id %s NOT NULL REFERENCES %s (%s),' % \ 
    99                 (f.rel.to.object_name.lower(), db.DATA_TYPES['IntegerField'], f.rel.to.db_table, f.rel.to.pk.name)) 
     99                (f.rel.to.object_name.lower(), db.DATA_TYPES['IntegerField'], f.rel.to.db_table, f.rel.to.pk.column)) 
    100100            table_output.append('    UNIQUE (%s_id, %s_id)' % (opts.object_name.lower(), f.rel.to.object_name.lower())) 
    101101            table_output.append(');') 
     
    187187        for f in klass._meta.fields: 
    188188            if isinstance(f, meta.AutoField): 
    189                 output.append("SELECT setval('%s_%s_seq', (SELECT max(%s) FROM %s));" % (klass._meta.db_table, f.name, f.name, klass._meta.db_table)) 
     189                output.append("SELECT setval('%s_%s_seq', (SELECT max(%s) FROM %s));" % (klass._meta.db_table, f.column, f.column, klass._meta.db_table)) 
    190190    return output 
    191191get_sql_sequence_reset.help_doc = "Prints the SQL statements for resetting PostgreSQL sequences for the given app(s)." 
     
    200200                unique = f.unique and "UNIQUE " or "" 
    201201                output.append("CREATE %sINDEX %s_%s ON %s (%s);" % \ 
    202                     (unique, klass._meta.db_table, f.name, klass._meta.db_table, f.name)) 
     202                    (unique, klass._meta.db_table, f.column, klass._meta.db_table, f.column)) 
    203203    return output 
    204204get_sql_indexes.help_doc = "Prints the CREATE INDEX SQL statements for the given app(s)." 
     
    491491    def add(self, opts, error): 
    492492        self.errors.append((opts, error)) 
    493         self.outfile.write("%s.%s: %s\n" % (opts.module_name, opts.object_name, error)) 
     493        self.outfile.write("%s.%s: %s\n" % (opts.app_label, opts.module_name, error)) 
    494494 
    495495def validate(): 
     
    525525                    if field_name.startswith('-'): 
    526526                        field_name = field_name[1:] 
     527                    if opts.order_with_respect_to and field_name == '_order': 
     528                        continue 
    527529                    try: 
    528530                        opts.get_field(field_name, many_to_many=False) 
  • django/trunk/django/core/meta/fields.py

    r530 r549  
    4141    except ObjectDoesNotExist: 
    4242        return 
    43     if hasattr(self, 'original_object') and getattr(self.original_object, opts.pk.name) == getattr(old_obj, opts.pk.name): 
     43    if hasattr(self, 'original_object') and getattr(self.original_object, opts.pk.column) == getattr(old_obj, opts.pk.column): 
    4444        return 
    4545    raise validators.ValidationError, "%s with this %s already exists." % (capfirst(opts.verbose_name), f.verbose_name) 
     
    5151    empty_strings_allowed = True 
    5252 
    53     def __init__(self, name, verbose_name=None, primary_key=False, 
     53    # Tracks each time a Field instance is created. Used to retain order. 
     54    creation_counter = 0 
     55 
     56    def __init__(self, verbose_name=None, name=None, primary_key=False, 
    5457        maxlength=None, unique=False, blank=False, null=False, db_index=None, 
    5558        core=False, rel=None, default=NOT_PROVIDED, editable=True, 
    5659        prepopulate_from=None, unique_for_date=None, unique_for_month=None, 
    5760        unique_for_year=None, validator_list=None, choices=None, radio_admin=None, 
    58         help_text=''): 
     61        help_text='', db_column=None): 
    5962        self.name = name 
    60         self.verbose_name = verbose_name or name.replace('_', ' '
     63        self.verbose_name = verbose_name or (name and name.replace('_', ' ')
    6164        self.primary_key = primary_key 
    6265        self.maxlength, self.unique = maxlength, unique 
     
    7174        self.radio_admin = radio_admin 
    7275        self.help_text = help_text 
     76        self.db_column = db_column 
    7377        if rel and isinstance(rel, ManyToMany): 
    7478            if rel.raw_id_admin: 
     
    8589        else: 
    8690            self.db_index = db_index 
     91 
     92        # Increase the creation counter, and save our local copy. 
     93        self.creation_counter = Field.creation_counter 
     94        Field.creation_counter += 1 
     95 
     96        # Set the name of the database column. 
     97        self.column = self.get_db_column() 
     98 
     99    def set_name(self, name): 
     100        self.name = name 
     101        self.verbose_name = self.verbose_name or name.replace('_', ' ') 
     102        self.column = self.get_db_column() 
     103 
     104    def get_db_column(self): 
     105        if self.db_column: return self.db_column 
     106        if isinstance(self.rel, ManyToOne): 
     107            return '%s_id' % self.name 
     108        return self.name 
     109 
     110    def get_cache_name(self): 
     111        return '_%s_cache' % self.name 
    87112 
    88113    def pre_save(self, value, add): 
     
    233258            return first_choice + list(self.choices) 
    234259        rel_obj = self.rel.to 
    235         return first_choice + [(getattr(x, rel_obj.pk.name), repr(x)) for x in rel_obj.get_model_module().get_list(**self.rel.limit_choices_to)] 
     260        return first_choice + [(getattr(x, rel_obj.pk.column), repr(x)) for x in rel_obj.get_model_module().get_list(**self.rel.limit_choices_to)] 
    236261 
    237262class AutoField(Field): 
     
    272297class DateField(Field): 
    273298    empty_strings_allowed = False 
    274     def __init__(self, name, verbose_name=None, auto_now=False, auto_now_add=False, **kwargs): 
     299    def __init__(self, verbose_name=None, name=None, auto_now=False, auto_now_add=False, **kwargs): 
    275300        self.auto_now, self.auto_now_add = auto_now, auto_now_add 
    276301        if auto_now or auto_now_add: 
    277302            kwargs['editable'] = False 
    278         Field.__init__(self, name, verbose_name, **kwargs) 
     303        Field.__init__(self, verbose_name, name, **kwargs) 
    279304 
    280305    def get_db_prep_lookup(self, lookup_type, value): 
     
    333358 
    334359class FileField(Field): 
    335     def __init__(self, name, verbose_name=None, upload_to='', **kwargs): 
     360    def __init__(self, verbose_name=None, name=None, upload_to='', **kwargs): 
    336361        self.upload_to = upload_to 
    337         Field.__init__(self, name, verbose_name, **kwargs) 
     362        Field.__init__(self, verbose_name, name, **kwargs) 
    338363 
    339364    def get_manipulator_fields(self, opts, manipulator, change, name_prefix='', rel=False): 
     
    398423class FloatField(Field): 
    399424    empty_strings_allowed = False 
    400     def __init__(self, name, verbose_name=None, max_digits=None, decimal_places=None, **kwargs): 
     425    def __init__(self, verbose_name=None, name=None, max_digits=None, decimal_places=None, **kwargs): 
    401426        self.max_digits, self.decimal_places = max_digits, decimal_places 
    402         Field.__init__(self, name, verbose_name, **kwargs) 
     427        Field.__init__(self, verbose_name, name, **kwargs) 
    403428 
    404429    def get_manipulator_field_objs(self): 
     
    406431 
    407432class ImageField(FileField): 
    408     def __init__(self, name, verbose_name=None, width_field=None, height_field=None, **kwargs): 
     433    def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, **kwargs): 
    409434        self.width_field, self.height_field = width_field, height_field 
    410         FileField.__init__(self, name, verbose_name, **kwargs) 
     435        FileField.__init__(self, verbose_name, name, **kwargs) 
    411436 
    412437    def get_manipulator_field_objs(self): 
     
    480505class TimeField(Field): 
    481506    empty_strings_allowed = False 
    482     def __init__(self, name, verbose_name=None, auto_now=False, auto_now_add=False, **kwargs): 
     507    def __init__(self, verbose_name=None, name=None, auto_now=False, auto_now_add=False, **kwargs): 
    483508        self.auto_now, self.auto_now_add  = auto_now, auto_now_add 
    484509        if auto_now or auto_now_add: 
    485510            kwargs['editable'] = False 
    486         Field.__init__(self, name, verbose_name, **kwargs) 
     511        Field.__init__(self, verbose_name, name, **kwargs) 
    487512 
    488513    def get_db_prep_lookup(self, lookup_type, value): 
     
    512537 
    513538class URLField(Field): 
    514     def __init__(self, name, verbose_name=None, verify_exists=True, **kwargs): 
     539    def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs): 
    515540        if verify_exists: 
    516541            kwargs.setdefault('validator_list', []).append(validators.isExistingURL) 
    517         Field.__init__(self, name, verbose_name, **kwargs) 
     542        Field.__init__(self, verbose_name, name, **kwargs) 
    518543 
    519544    def get_manipulator_field_objs(self): 
     
    525550 
    526551class XMLField(Field): 
    527     def __init__(self, name, verbose_name=None, schema_path=None, **kwargs): 
     552    def __init__(self, verbose_name=None, name=None, schema_path=None, **kwargs): 
    528553        self.schema_path = schema_path 
    529         Field.__init__(self, name, verbose_name, **kwargs) 
     554        Field.__init__(self, verbose_name, name, **kwargs) 
    530555 
    531556    def get_manipulator_field_objs(self): 
     
    534559class ForeignKey(Field): 
    535560    empty_strings_allowed = False 
    536     def __init__(self, to, to_field=None, rel_name=None, **kwargs): 
     561    def __init__(self, to, to_field=None, **kwargs): 
    537562        try: 
    538563            to_name = to._meta.object_name.lower() 
    539564        except AttributeError: # to._meta doesn't exist, so it must be RECURSIVE_RELATIONSHIP_CONSTANT 
    540565            assert to == 'self', "ForeignKey(%r) is invalid. First parameter to ForeignKey must be either a model or the string %r" % (to, RECURSIVE_RELATIONSHIP_CONSTANT) 
    541             kwargs['name'] = kwargs.get('name', '') 
    542566            kwargs['verbose_name'] = kwargs.get('verbose_name', '') 
    543567        else: 
    544568            to_field = to_field or to._meta.pk.name 
    545             kwargs['name'] = kwargs.get('name', to_name + '_id') 
    546569            kwargs['verbose_name'] = kwargs.get('verbose_name', to._meta.verbose_name) 
    547             rel_name = rel_name or to_name 
    548570 
    549571        if kwargs.has_key('edit_inline_type'): 
     
    552574            kwargs['edit_inline'] = kwargs.pop('edit_inline_type') 
    553575 
    554         kwargs['rel'] = ManyToOne(to, rel_name, to_field, 
     576        kwargs['rel'] = ManyToOne(to, to_field, 
    555577            num_in_admin=kwargs.pop('num_in_admin', 3), 
    556578            min_num_in_admin=kwargs.pop('min_num_in_admin', None), 
     
    568590 
    569591class ManyToManyField(Field): 
    570     def __init__(self, to, rel_name=None, **kwargs): 
    571         kwargs['name'] = kwargs.get('name', to._meta.module_name) 
     592    def __init__(self, to, **kwargs): 
    572593        kwargs['verbose_name'] = kwargs.get('verbose_name', to._meta.verbose_name_plural) 
    573         rel_name = rel_name or to._meta.object_name.lower() 
    574         kwargs['rel'] = ManyToMany(to, rel_name, 
     594        kwargs['rel'] = ManyToMany(to, kwargs.pop('singular', None), 
    575595            num_in_admin=kwargs.pop('num_in_admin', 0), 
    576596            related_name=kwargs.pop('related_name', None), 
     
    610630 
    611631class OneToOneField(IntegerField): 
    612     def __init__(self, to, to_field=None, rel_name=None, **kwargs): 
    613         kwargs['name'] = kwargs.get('name', 'id') 
     632    def __init__(self, to, to_field=None, **kwargs): 
    614633        kwargs['verbose_name'] = kwargs.get('verbose_name', 'ID') 
    615634        to_field = to_field or to._meta.pk.name 
    616         rel_name = rel_name or to._meta.object_name.lower() 
    617635 
    618636        if kwargs.has_key('edit_inline_type'): 
     
    621639            kwargs['edit_inline'] = kwargs.pop('edit_inline_type') 
    622640 
    623         kwargs['rel'] = OneToOne(to, rel_name, to_field, 
     641        kwargs['rel'] = OneToOne(to, to_field, 
    624642            num_in_admin=kwargs.pop('num_in_admin', 0), 
    625643            edit_inline=kwargs.pop('edit_inline', False), 
     
    632650 
    633651class ManyToOne: 
    634     def __init__(self, to, name, field_name, num_in_admin=3, min_num_in_admin=None, 
     652    def __init__(self, to, field_name, num_in_admin=3, min_num_in_admin=None, 
    635653        max_num_in_admin=None, num_extra_on_change=1, edit_inline=False, 
    636654        related_name=None, limit_choices_to=None, lookup_overrides=None, raw_id_admin=False): 
     
    640658            assert to == RECURSIVE_RELATIONSHIP_CONSTANT, "'to' must be either a model or the string '%s'" % RECURSIVE_RELATIONSHIP_CONSTANT 
    641659            self.to = to 
    642         self.name, self.field_name = name, field_name 
     660        self.field_name = field_name 
    643661        self.num_in_admin, self.edit_inline = num_in_admin, edit_inline 
    644662        self.min_num_in_admin, self.max_num_in_admin = min_num_in_admin, max_num_in_admin 
     
    648666        self.raw_id_admin = raw_id_admin 
    649667 
    650     def get_cache_name(self): 
    651         return '_%s_cache' % self.name 
    652  
    653668    def get_related_field(self): 
    654669        "Returns the Field in the 'to' object to which this relationship is tied." 
     
    656671 
    657672class ManyToMany: 
    658     def __init__(self, to, name, num_in_admin=0, related_name=None, 
     673    def __init__(self, to, singular=None, num_in_admin=0, related_name=None, 
    659674        filter_interface=None, limit_choices_to=None, raw_id_admin=False): 
    660         self.to, self.name = to._meta, name 
     675        self.to = to._meta 
     676        self.singular = singular or to._meta.object_name.lower() 
    661677        self.num_in_admin = num_in_admin 
    662678        self.related_name = related_name 
     
    668684 
    669685class OneToOne(ManyToOne): 
    670     def __init__(self, to, name, field_name, num_in_admin=0, edit_inline=False, 
     686    def __init__(self, to, field_name, num_in_admin=0, edit_inline=False, 
    671687        related_name=None, limit_choices_to=None, lookup_overrides=None, 
    672688        raw_id_admin=False): 
    673         self.to, self.name, self.field_name = to._meta, name, field_name 
     689        self.to, self.field_name = to._meta, field_name 
    674690        self.num_in_admin, self.edit_inline = num_in_admin, edit_inline 
    675691        self.related_name = related_name 
  • django/trunk/django/core/meta/__init__.py

    r541 r549  
    5151        return new_order_list 
    5252 
    53 def orderlist2sql(order_list, prefix=''): 
     53def orderfield2column(f, opts): 
     54    try: 
     55        return opts.get_field(f, False).column 
     56    except FieldDoesNotExist: 
     57        return f 
     58 
     59def orderlist2sql(order_list, opts, prefix=''): 
    5460    output = [] 
    5561    for f in handle_legacy_orderlist(order_list): 
    5662        if f.startswith('-'): 
    57             output.append('%s%s DESC' % (prefix, f[1:])) 
     63            output.append('%s%s DESC' % (prefix, orderfield2column(f[1:], opts))) 
    5864        elif f == '?': 
    5965            output.append('RANDOM()') 
    6066        else: 
    61             output.append('%s%s ASC' % (prefix, f)) 
     67            output.append('%s%s ASC' % (prefix, orderfield2column(f, opts))) 
    6268    return ', '.join(output) 
    6369 
     
    207213        # auto-incrementing primary-key ID field automatically. 
    208214        if self.pk is None: 
    209             self.fields.insert(0, AutoField('id', 'ID', primary_key=True))