Code

Opened 4 years ago

Closed 4 years ago

#14036 closed (invalid)

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

Reported by: nemesisdesign Owned by: nobody
Component: contrib.admin Version: master
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: UI/UX:

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?

Attachments (0)

Change History (2)

comment:1 Changed 4 years ago by nemesisdesign

  • Component changed from Uncategorized to django.contrib.admin
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 4 years ago by kmtracey

  • Resolution set to invalid
  • Status changed from new to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.