Opened 14 years ago

Closed 14 years ago

#14036 closed (invalid)

foreignKey doesn't work properly in admin - last current svn revision (1.3 pre alpha)

Reported by: Federico Capoano Owned by: nobody
Component: contrib.admin Version: dev
Severity: Keywords: foreignKey foreign key id
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Trying to save an item in the admin that has a simple foreignKey I receive an error saying I break null constraint "id_category" - maybe should be category_id? Downgrading to revision 13315 fixed the problem.

This is the part of model interested:

class Category(OrderedModel):
    """ Categories for the locations """
       
    name = models.CharField(max_length=50)
    meta_description = models.CharField('meta description', max_length=160, blank=True, help_text='Max 160 characters.')
    meta_keywords = models.CharField('meta keywords', max_length=20, blank=True)

    created = models.DateTimeField('created', auto_now_add=True)
    modified = models.DateTimeField('modified', auto_now=True)
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        db_table='locations_category'
        verbose_name = 'Category'
        verbose_name_plural = 'Categories'
        #ordering = ('ordering','created')
        
class Location(OrderedModel):
    """ Categories for the locations """
    
    category = models.ForeignKey(Category)
    name = models.CharField(max_length=100)
    description = models.TextField()
    meta_description = models.CharField('meta description', max_length=160, blank=True, help_text='Max 160 characters.')
    meta_keywords = models.CharField('meta keywords', max_length=20, blank=True)

    created = models.DateTimeField('created', auto_now_add=True)
    modified = models.DateTimeField('modified', auto_now=True)
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        db_table='locations_location'
        verbose_name = 'Location'
        verbose_name_plural = 'Locations'
        #ordering = ('ordering','created')

When in the admin I tried to save a location i got that error.
Probably you already noticed?

Change History (2)

comment:1 by Federico Capoano, 14 years ago

Component: Uncategorizeddjango.contrib.admin

comment:2 by Karen Tracey, 14 years ago

Resolution: invalid
Status: newclosed

There is not enough information here to recreate the problem (and I have not seen anything like what you mention in my own testing).

The provided models are based off of one that is not provided -- OrderedModel -- and there are no admin definitions included. Basing the provided models off of models.Model and doing a basic admin.site.register for these models, I cannot recreate any problem.

When adding a Location, leaving the ForeignKey field "blank" (----- choice in the select dropdown), and attempting to save that causes the admin to re-display the add page with a message "This field is required." associated with the Category field. Choosing an existing Category instead results in the save working properly. So I don't see any messages about "id_category" (or "category_id") being null.

The description is also not very specific about what you are doing, exactly -- are you actually trying to have the foreign key be blank/null, and complaining that the resulting message is not quite correct? Or are you supplying a valid choice? I'm also not entirely sure from the description if you are just getting a form validation error message displayed when you submit the form or if you are getting a traceback from an exception. If you are getting a traceback, including the full traceback and not just the message from it would be helpful.

Finally it would help track down the problem if you can identify the exact changeset that introduced the behavior you are seeing. You say it fails on current svn but works on r13315. However r13316 could not possibly have introduced the problem since it changes documentation only.

Note: See TracTickets for help on using tickets.
Back to Top