How to fix Field defines a relation with the model auth.User, which has been swapped out

2024/9/29 19:24:47

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.idexcept (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.

Answer

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

https://en.xdnf.cn/q/71171.html

Related Q&A

Generate and parse Python code from C# application

I need to generate Python code to be more specific IronPyton. I also need to be able to parse the code and to load it into AST. I just started looking at some tools. I played with "Oslo" and …

An efficient way to calculate the mean of each column or row of non-zero elements

I have a numpy array for ratings given by users on movies. The rating is between 1 and 5, while 0 means that a user does not rate on a movie. I want to calculate the average rating of each movie, and t…

Selecting unique observations in a pandas data frame

I have a pandas data frame with a column uniqueid. I would like to remove all duplicates from the data frame based on this column, such that all remaining observations are unique.

GEdit/Python execution plugin?

Im just starting out learning python with GEdit plus various plugins as my IDE.Visual Studio/F# has a feature which permits the highlighting on a piece of text in the code window which then, on a keyp…

autoclass and instance attributes

According to the sphinx documentation, the .. autoattribute directive should be able to document instance attributes. However, if I do::.. currentmodule:: xml.etree.ElementTree.. autoclass:: ElementTre…

python sqlite3 update not updating

Question: Why is this sqlite3 statement not updating the record?Info:cur.execute(UPDATE workunits SET Completed=1 AND Returns=(?) WHERE PID=(?) AND Args=(?),(pickle.dumps(Ret),PID,Args))Im using py…

Unable to reinstall PyTables for Python 2.7

I am installing Python 2.7 in addition to 2.7. When installing PyTables again for 2.7, I get this error -Found numpy 1.5.1 package installed. .. ERROR:: Could not find a local HDF5 installation. You ma…

How can I invoke a thread multiple times in Python?

Im sorry if it is a stupid question. I am trying to use a number of classes of multi-threading to finish different jobs, which involves invoking these multi-threadings at different times for many times…

Matplotlib interactive graph embedded in PyQt

Ive created a simple python script that when run should display an embedded matplotlib graph inside a PyQT window. Ive used this tutorial for embedding and running the graph. Aside from some difference…

How to pass path names to Python script by dropping files/folders over script icon

I am working in Mac OS X and have been writing simple file/folder copy scripts in Python. Is there a way to drag and drop a folder on top of a Python script icon and pass the file or folders path as an…