Raise CommandError: One or more models did not validate

409 views
Skip to first unread message

Petey

unread,
Jul 14, 2011, 5:30:28 PM7/14/11
to django...@googlegroups.com
Hey

I'am working on my models page and this is my code:
http://pastebin.com/eAyzzZuY

which returns an error:
django.core.management.base.CommandError: One or more models did not validate:
news.news: Accessor for field 'created' clashes with related field 'User.news_se
t'. Add a related_name argument to the definition for 'created'.
news.news: Accessor for field 'edited' clashes with related field 'User.news_set
'. Add a related_name argument to the definition for 'edited'.

I know that without "edited" part it works perfectly. I dont really understand why it cant work with an additional "edited" field.

How do I fix this this? What should I look for?

Tim Shaffer

unread,
Jul 14, 2011, 5:34:45 PM7/14/11
to django...@googlegroups.com
When you do this:

    created = models.ForeignKey(User, default=user_logged_in, editable=False)

It's going to create a method on the user instance called "news_set".

Then when you then do this:

    edited = models.ForeignKey(User, default=user_logged_in, editable=False)

It's going to try to create *another* method on the user instance called "news_set". Obviously that clashes with the first one created, and you get the error.

To get around this, you can give "created" and "edited" a distinct related_name that will be used instead of "news_set"

   
created = models.ForeignKey(User, default=user_logged_in, editable=False, related_name="news_created_set")
   
edited = models.ForeignKey(User, default=user_logged_in, editable=False, related_name="news_edited_set")

This is mentioned in the documentation here:

https://docs.djangoproject.com/en/dev/topics/db/models/#be-careful-with-related-name

Petey

unread,
Jul 14, 2011, 6:11:14 PM7/14/11
to django...@googlegroups.com

I got an error even after removing my modifications to the source:

ValueError at /admin/news/news/add/

invalid literal for int() with base 10: '<django.dispatch.dispatcher.Signal object at 0x02CFCC90>'


Tim Shaffer

unread,
Jul 14, 2011, 6:14:31 PM7/14/11
to django...@googlegroups.com
I just noticed that you had the default value set to the "user_logged_in" signal method. That's not going to work at all. You probably want to remove that from the field definition.

Petey

unread,
Jul 14, 2011, 6:19:44 PM7/14/11
to django...@googlegroups.com
The target of this is to automatically fill fields without users having to chose "user".

Petey

unread,
Jul 14, 2011, 6:32:26 PM7/14/11
to django...@googlegroups.com
Is there a way around?

Petey

unread,
Jul 14, 2011, 6:38:53 PM7/14/11
to django...@googlegroups.com
Sorry for multiple messages but I think that I've accidently fixed it:

from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.signals import user_logged_in
import datetime

class News(models.Model):
    text = models.TextField()
    title = models.TextField()
    date = models.DateTimeField(editable=False, default=datetime.datetime.now())
    source_name = models.CharField(max_length=50)
    created = models.ForeignKey(User, default=user_logged_in, related_name="news_created_set")
    edited = models.ForeignKey(User, default=user_logged_in, related_name="news_edited_set")

     
    class Meta:
        verbose_name_plural = "News"
        abstract = False


Tim Shaffer

unread,
Jul 14, 2011, 6:54:13 PM7/14/11
to django...@googlegroups.com
You should still probably remove the user_logged_in from your default value. It doesn't do what you think it does, and could cause other strange things to happen.

Tim Shaffer

unread,
Jul 14, 2011, 6:56:42 PM7/14/11
to django...@googlegroups.com
On Thursday, July 14, 2011 2:32:26 PM UTC-4, Petey wrote:
Is there a way around?

This isn't really something that can/should be solved in the model itself.

You could try overwriting the formfield_for_foreignkey method in your ModelAdmin:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey

Petey

unread,
Jul 14, 2011, 7:01:54 PM7/14/11
to django...@googlegroups.com
I am just starting to do adminmodel and now I've noticed that it can be done by this function and not models :)
I'll work a bit more on that tomorrow :)

Thanks for help I really appreciate it.

Reply all
Reply to author
Forward
0 new messages