7

I dont understand why some fields of my models clash.

I dont have any foreign key so why would they clash ?!

Here is my code:

from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import AbstractUser
import datetime
import uuid

# Create your models here
class Patients(AbstractUser):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    first_name = models.CharField(max_length = 255)
    last_name = models.CharField(max_length = 255)
    dob = models.DateField(datetime.date.today)
    gender = models.CharField(max_length = 1)
    def __unicode__(self):
        return self.id

Here is the error:

api.Patients.groups: (fields.E304) Reverse accessor for 'Patients.groups' clashes with reverse accessor for 'User.groups'.
        HINT: Add or change a related_name argument to the definition for 'Patients.groups' or 'User.groups'.
api.Patients.user_permissions: (fields.E304) Reverse accessor for 'Patients.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'Patients.user_permissions' or 'User.user_permissions'.
auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'Patients.groups'.
        HINT: Add or change a related_name argument to the definition for 'User.groups' or 'Patients.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'Patients.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'Patients.user_permissions'.
0xtuytuy
  • 1,494
  • 6
  • 26
  • 51

2 Answers2

15

You need to add AUTH_USER_MODEL to your setting.py file. Django needs to know that to initialise the default model. You can add that as follows:

AUTH_USER_MODEL = 'your_app.Patients'

Check this in the documentation Substituting a custom User model

Reference: https://stackoverflow.com/a/26703434/4575071

Community
  • 1
  • 1
ettanany
  • 19,038
  • 9
  • 47
  • 63
  • what should i put in the [] just the name of my model ? so this: AUTH_USER_MODEL = [Patient] ? – 0xtuytuy Nov 28 '16 at 11:47
  • 1
    Your app name, is it `api`? if so then you will add `api.Patients` – ettanany Nov 28 '16 at 11:48
  • It is api indeed but now i get this error: ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'api.patients', but app 'api' doesn't provide model 'patients'. – 0xtuytuy Nov 28 '16 at 12:00
  • It should be `api.Patients` not `api.patients` – ettanany Nov 28 '16 at 12:04
  • yea sorry i noticed, even with api.Partients it does not work same error – 0xtuytuy Nov 28 '16 at 12:05
  • Do you have some old migrations? you may need to remove them. – ettanany Nov 28 '16 at 12:06
  • I have just ran makemigrations then migrate again but still nothing – 0xtuytuy Nov 28 '16 at 12:06
  • remove the folder migrations in your `api` app, then run `python manage.py makemigrations api` and `python manage.py migrate` – ettanany Nov 28 '16 at 12:07
  • i guess the typo is only on here and not in your code, but just in case, you wrote 'api.Partients' – Brian H. Nov 28 '16 at 12:07
  • @ettanany that worked but i get another error ... I can run migrate fine but when i try to access my api through the browser using Http://127.0.0.0:8000/patients/patient i get the following error: OperationalError: no such table: api_patients – 0xtuytuy Nov 28 '16 at 12:13
  • Are you sure you run `python manage.py makemigrations api`? your database is configured correctly? – ettanany Nov 28 '16 at 12:14
  • yes i did, that worked fine, i then tried to run python manage.py migrate and i am asked if i want to delete the content of auth and user because they are stale – 0xtuytuy Nov 28 '16 at 12:18
  • here is the exact error message: The following content types are stale and need to be deleted: auth | user Any objects related to these content types by a foreign key will also be deleted. Are you sure you want to delete these content types? If you're unsure, answer 'no'. Type 'yes' to continue, or 'no' to cancel: – 0xtuytuy Nov 28 '16 at 12:18
  • If your data is not important you can choose `yes` or even better delete your database if it is just for development and start all over again. – ettanany Nov 28 '16 at 12:20
  • right i have sqlflush and i can now migrate, but i still get the error OperationalError: no such table: api_patients – 0xtuytuy Nov 28 '16 at 12:23
  • 1
    This is an error different from what was mentioned in your question, I think it would be better to ask another question if the above issue is resolved. – ettanany Nov 28 '16 at 12:25
2

is using AbstractUser, then you should use into file settings.py:

AUTH_USER_MODEL = 'user.user'