Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#13717 closed (worksforme)

unique_for_* errors out when date field is left empty

Reported by: silviogutierrez Owned by: suzaku
Component: contrib.admin Version: 1.2
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: UI/UX:


Using Django 1.2.1, create a simple model, like so:

from django.db import models

class Article(models.Model):

    title = models.CharField(max_length=200)
    pub_date = models.DateField()
    slug = models.SlugField(max_length=150, unique_for_year='pub_date')    

and register it with the admin site in; the standard registration is fine:

from django.contrib import admin

from models import Article

Go to create an article, and enter "abc" as the tile and the slug, but leave the date empty. On submission, you'll get 'NoneType' object has no attribute 'day' .

The error seems to apply to unique_for_year and unique_for_month as well, though the error displays "no attribute 'year'" and "no attribute 'month'" respectively, naturally. No error occurs if the field marked as unique_for_* is left empty, in this case the slug. Rather the actual validation message comes up. My guess is that the date field needs to be checked to see if it's empty first, and then check for uniqueness.

In short, rather than displaying the validation for "this field is required" which is user friendly, the submission errors out. The same setup does not error out on Django 1.1.2.

Let me know if you need any more details,


Attachments (1)

13717_fix_unique_for.diff (1.3 KB) - added by suzaku 6 years ago.
Make sure the value of the unique_for field is not None before making the query.

Download all attachments as: .zip

Change History (7)

comment:1 Changed 6 years ago by silviogutierrez

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

comment:2 Changed 6 years ago by suzaku

  • Owner changed from nobody to suzaku
  • Status changed from new to assigned

Changed 6 years ago by suzaku

Make sure the value of the unique_for field is not None before making the query.

comment:3 follow-up: Changed 6 years ago by Alex

  • Needs tests set

comment:4 in reply to: ↑ 3 Changed 6 years ago by suzaku

Replying to Alex:

Hi, Alex.
I'm not quite sure where should I put my test code in, could you please tell me?

comment:5 Changed 6 years ago by rasca

  • Resolution set to worksforme
  • Status changed from assigned to closed

Hi, this works for me as of 14551

comment:6 Changed 6 years ago by natrius

I was still getting non-producible errors after this bug was closed, but the fix for bug #14951 should definitely fix it.

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