Need help getting this snippet to work in Django 1.2...

15 views
Skip to first unread message

mhulse

unread,
Jun 10, 2011, 9:53:44 PM6/10/11
to django...@googlegroups.com
I feel like this should be simple...

I would like to replace this model field:

date = models.DateField(_(u'Year'), unique=True)

...with this snippet:

http://djangosnippets.org/snippets/508/

I have spent the last couple hours banging my head on my keyboard...

During the course of my tests, at one point, I was getting an error that said something like "__init__() got an unexpected keyword argument 'unique'" (I think I was trying to extend IntegerField).

Any tips on how to convert that snippet so I can do:

from app.fields import YearField
...
year = YearField(_(u'Year'), unique=True)
...

Normally I wouldn't ask, but I am ready to give up... At this point, I am just curious to see how it's done for the sake of learning something new.

Thanks so much!

Cheers,
Micky

Martin

unread,
Jun 10, 2011, 10:27:12 PM6/10/11
to django...@googlegroups.com
I think this is not how it works. The snippet is a form field, not a model field. Basically I think you should use an IntegerField (you just want to store a year, right?). Then somehow you need to override the widget or so and make sure that in the admin that form will be used for that IntegerField. This form field basically just makes sure, that people don't enter anything stupid, but what is being saved will just be a normal integer, I guess.

Sorry, don't have enough time to give a better hint.

Best regards,
Martin

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/Ov8sCyIqUUIJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

mhulse

unread,
Jun 10, 2011, 11:25:26 PM6/10/11
to django...@googlegroups.com
Hi Martin! Thanks so much for the help, I really appreciate it. :)


> The snippet is a form field, not a model field.

Well, that would explain things! Lol.

When I read this in the instructions:

"Usage eg: yob = BirthYearField(label="What year were you born?")"

I thought that was a model field.

Thanks for pointing me in the right direction! :)

Have a nice day.

Cheers,
Micky

Alex Kamedov

unread,
Jun 13, 2011, 12:01:49 AM6/13/11
to django...@googlegroups.com
It'll be better to use validators with IntegerField

In this case you can specify allowed year range.




Cheers,
Micky

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/BPbNW-I4VC8J.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Alex Kamedov
skype: kamedov    www: kamedov.ru

Micky Hulse

unread,
Jun 13, 2011, 12:44:35 PM6/13/11
to django...@googlegroups.com
Hi Alex! Thanks for the reply, I really appreciate it. :)

On Sun, Jun 12, 2011 at 9:01 PM, Alex Kamedov <kam...@gmail.com> wrote:
> It'll be better to use validators with IntegerField

> In this case you can specify allowed year range.

Ooh, great tip! That looks very useful and possibly a little more
simple/straight-forward than using a form field.

Much appreciated.

Have a great day!

Cheers,
Micky

Micky Hulse

unread,
Jun 13, 2011, 2:06:03 PM6/13/11
to django...@googlegroups.com
On Mon, Jun 13, 2011 at 9:44 AM, Micky Hulse <rgm...@gmail.com> wrote:
> Ooh, great tip! That looks very useful and possibly a little more
> simple/straight-forward than using a form field.

Too easy!

https://gist.github.com/1023327

Thanks for the help Martin and Alex, I really appreciate it! :)

Have an awesome day.

Cheers,
Micky

Reply all
Reply to author
Forward
0 new messages