0

I have a project with one app, where user, their posts and other models are located. Now I want to separate User model and put it into different app, called users.
How should I do this correctly? And how do I use this model in the old app?
User model of the first app looks like this:

class User(AbstractUser):

    class Meta:
        permissions = (
            ('can_do_this', 'Permission1'),
            ('can_do_that', 'Permission2'),
            ...,
        )

User model of the second (users)app looks like this:

class ExternalUser(AbstractUser):

    class Meta:
        permissions = (
            ('can_do_this', 'Permission1'),
            ('can_do_that', 'Permission2'),
            ...,
        )

I've done django-admin startapp users and copy-pasted user's model code into created app models.py.

Then I tried makemigrations and it failed, showing:

SystemCheckError: System check identified some issues:

ERRORS:
photogal.User.groups: (fields.E304) Reverse accessor for 'User.groups' 
clashes with reverse accessor for 'ExternalUser.groups'.
    HINT: Add or change a related_name argument to the definition for 
'User.groups' or 'ExternalUser.groups'.
photogal.User.user_permissions: (fields.E304) Reverse accessor for 
'User.user_permissions' clashes with reverse accessor for 
'ExternalUser.user_permissions'.
    HINT: Add or change a related_name argument to the definition for 
'User.user_permissions' or 'ExternalUser.user_permissions'.
 users.ExternalUser.groups: (fields.E304) Reverse accessor for 
'ExternalUser.groups' clashes with reverse accessor for 'User.groups'.
    HINT: Add or change a related_name argument to the definition for 
'ExternalUser.groups' or 'User.groups'.
users.ExternalUser.user_permissions: (fields.E304) Reverse accessor for 
'ExternalUser.user_permissions' clashes with reverse accessor for 
'User.user_permissions'.
    HINT: Add or change a related_name argument to the definition for 
'ExternalUser.user_permissions' or 'User.user_permissions'.

Adding related_name='+' to the old user model resulted in:

ERRORS:
<class 'django.contrib.auth.admin.UserAdmin'>: (admin.E020) The value of 
'filter_horizontal[0]' must be a many-to-many field.
<class 'django.contrib.auth.admin.UserAdmin'>: (admin.E020) The value of 
'filter_horizontal[1]' must be a many-to-many field.

Thanks!

  • For starters, your `Meta` class should be indented inside of the parent class – Thomas Gak-Deluen Jan 24 '18 at 15:49
  • can you please post the model code for both the apps and also where you are importing it, that will be helpful. – Sumeet Kumar Jan 24 '18 at 15:50
  • @tgdn, fixed now. Thanks – Maxim Kolodnikov Jan 24 '18 at 15:51
  • Also here it seems that you have a class User, and a class ExternalUser which both inherit from AbstractUser? Why Don't you have a simple User class which handles whether or not it is an external User? Using a column or by just checking whether the id is set depending on what you are looking for. – Thomas Gak-Deluen Jan 24 '18 at 15:54
  • @sumeet-kumar I just copied User model from old app to new. That is the whole code for User, basically. Other models use it as ForeignKey. – Maxim Kolodnikov Jan 24 '18 at 15:56
  • @tgdn Ideally I want to relocate User model into different app(users) and get rid of the old one. – Maxim Kolodnikov Jan 24 '18 at 16:02
  • this seems to be simple. you need to import the new user model where you are using that as the foreign key. and remove the other one – Sumeet Kumar Jan 24 '18 at 16:04
  • @sumeet-kumar, How do I import it(like from ..users.models)? And what about related_name, I won't need it then? – Maxim Kolodnikov Jan 24 '18 at 16:06
  • Just delete the old User model from the previous app, and put the new one in the new app, so that in the end you only have one User class that extends the AbstractUser class – Thomas Gak-Deluen Jan 24 '18 at 16:09
  • that is why i was asking you to post complete code. where you are using it as foreign key. for the hint, you should import new model as `from app_name.models import ModelName` – Sumeet Kumar Jan 24 '18 at 16:09
  • @tgdn, thanks, that worked! Makekigrations worked, but migrate command gave me this: django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency users.0001_initial on database 'default' – Maxim Kolodnikov Jan 24 '18 at 16:20
  • It seems you helped me with the initial problem, shouldn't you post it as an answer, so I can rate it? – Maxim Kolodnikov Jan 24 '18 at 16:21

1 Answers1

1

Simply delete the old User model from the previous app, and put the new one in the new app, so that in the end you only have one User class that extends the AbstractUser class.

As for your second issue, try this.

I hope this helps.

Thomas Gak-Deluen
  • 2,759
  • 2
  • 28
  • 38
  • Furthermore, if your second issue is still not fixed, try deleting the folder with all migrations, reset your database, and start with a fresh makemigrations – Thomas Gak-Deluen Jan 24 '18 at 16:27