Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django "Add a related_name argument to the definition" Error

Tags:

python

django

models.py

class BaseComment(models.Model):
    comment_author = models.ForeignKey(MyUser, related_name='written_comments')
    comment_content = models.CharField(max_length=500)
    comment_date = models.DateTimeField(auto_now_add=True)
    rating = models.IntegerField(default=0)
    users_voted = models.ManyToManyField(MyUser, related_name='voted_comments')

    class Meta:
        abstract = True
        ordering = ['-comment_date']        

    def __unicode__(self):
        return self.comment_author.email

class QuestionComment(BaseComment):
    question = models.ForeignKey(Question, related_name='comments')

class AnswerComment(BaseComment):
    answer = models.ForeignKey(Answer, related_name='comments')

This is my models.py. When I run syncdb, I get this error:

CommandError: One or more models did not validate:

app_quora.questioncomment: Accessor for field 'comment_author' clashes with rela
ted field 'MyUser.written_comments'. Add a related_name argument to the definiti
on for 'comment_author'.
app_quora.questioncomment: Reverse query name for field 'comment_author' clashes
 with related field 'MyUser.written_comments'. Add a related_name argument to th
e definition for 'comment_author'.
app_quora.questioncomment: Accessor for m2m field 'users_voted' clashes with rel
ated m2m field 'MyUser.voted_comments'. Add a related_name argument to the defin
ition for 'users_voted'.
app_quora.questioncomment: Reverse query name for m2m field 'users_voted' clashe
s with related m2m field 'MyUser.voted_comments'. Add a related_name argument to
 the definition for 'users_voted'.
app_quora.answercomment: Accessor for field 'comment_author' clashes with relate
d field 'MyUser.written_comments'. Add a related_name argument to the definition
 for 'comment_author'.
app_quora.answercomment: Reverse query name for field 'comment_author' clashes w
ith related field 'MyUser.written_comments'. Add a related_name argument to the 
definition for 'comment_author'.
app_quora.answercomment: Accessor for m2m field 'users_voted' clashes with relat
ed m2m field 'MyUser.voted_comments'. Add a related_name argument to the definit
ion for 'users_voted'.
app_quora.answercomment: Reverse query name for m2m field 'users_voted' clashes 
with related m2m field 'MyUser.voted_comments'. Add a related_name argument to t
he definition for 'users_voted'.

Well, the error tells me to add the "related_name" for both "comment_author" and "users_voted", which I ALREADY DID!!. I looked up similar questions on stackoverflow, but usually the issue was that people didn't have this "related_name" for clashing fields.

I added this fields, but am still getting this error... Can someone explain why?

Thanks :(((

like image 457
user2492270 Avatar asked Dec 13 '25 03:12

user2492270


1 Answers

The problem you are seeing is because of the ForeignKey and ManyToManyField fields in the BaseComment model. Because it is an abstract model, django does not create a table for it.

However, the derived models QuestionComment and AnswerComment which inherit the fields try to add the same fields defined by related_name (written_comments , voted_comments ) in the MyUser model. Because you can have only one field with the same name it gives you those errors.

A solution would be to not have BaseComment as an abstract model and let it have its own table/model.

Or, you could add these fields in derived models.

You can also try not specifying related_name and let django decide the names, but I'm not sure if this will work or not.

like image 196
Rohan Avatar answered Dec 14 '25 15:12

Rohan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!