Setting unique = True to ForeignKey has the same effect as using OneToOneField

I recently switched to Django 1.8.2 from 1.7, but I ran into small problems, for example in one of my models:

class Author(models.Model):
    author = models.ForeignKey(UserProfile, blank=False, primary_key=True)
    timestamp = models.DateTimeField(auto_now_add=True)

      

But when I start the server, I am faced with this following warning:

WARNINGS:
exam.Author.author: (fields.W342) Setting unique=True on a ForeignKey has the same effect as using a OneToOneField.
HINT: ForeignKey(unique=True) is usually better served by a OneToOneField.

      

What should I do?

+3


source to share


2 answers


primary_key

means unique=True

. So, as warns, you should probably be using OneToOneField.



+2


source


As Daniel said , you are probably better off using OneToOneField

.



A good explanation why this can be found in this question .

0


source







All Articles