I am trying to change my user model to a custom one. I do not mind dropping my database and just using a new one, but when I try it to run makemigrations i get this error
bookings.Session.session_client: (fields.E301) Field defines a relation with the model 'auth.User', which has been swapped out.
HINT: Update the relation to point at 'settings.AUTH_USER_MODEL'.
booking/views.py
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from bookings.models import Session
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
# Create your views here.
def book(request, Session_id):
lesson = get_object_or_404(Session, pk=Session_id)
try:
client = request.user.id
except (KeyError, Choice.DoesNotExist):
return render(request, 'booking/details.html', {
'lesson': lesson,
'error_message': "You need to login first",
})
else:
lesson.session_client.add(client)
lesson.save()
return HttpResponseRedirect("")
settings.py
INSTALLED_APPS = [
#Custom apps
'mainapp.apps.MainappConfig',
'bookings.apps.BookingsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
AUTH_USER_MODEL = 'mainapp.CustomUser'
bookings/models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Session(models.Model):
session_title = models.CharField(max_length=300)
session_time = models.DateTimeField("Time of session")
session_difficulty = models.CharField(max_length=200)
session_duration = models.IntegerField()
session_description = models.TextField()
session_client = models.ManyToManyField(User, blank=True)
def __str__(self):
return self.session_title
I'm trying to change the user model to abstractuser.
include the following at in views.py
from django.conf import settings
User = settings.AUTH_USER_MODEL
and remove from django.contrib.auth.models import User
The problem is this line in booking/models.py:
session_client = models.ManyToManyField(User, blank=True)
You have a ManyToManyField to User, but you have set a different User model.
So in that field remove the import of User, add
from django.conf import settings
and change the model line to
session_client = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True)
Edit: there's another similar problem in views.py, as HenryM notes.
Change:
from django.contrib.auth.models import User
To:
from django.contrib.auth import get_user_model
User = get_user_model()
because in settings.py is defined
AUTH_USER_MODEL = 'user.User'
instead of auth.User I added user.User
user = models.ForeignKey('user.User', on_delete=models.CASCADE)
I had the same error, Change model fields from this:
user = models.ForeignKey(to=User, on_delete=models.CASCADE)
to this:
user = models.ForeignKey(to=MyUser, on_delete=models.CASCADE)
MyUser is your custom user model
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With