When i add new data, it will come up with object (1). If i go in the table with the many to one relation, it will not show the ID and instead show the "object (1). I have tried changing the field sets however this does not work. When i go to make a "UserAdmin" it will treat it like it a user being added.
Admin.py
from lib2to3.pgen2.token import OP
from statistics import mode
from attr import field
from .forms import createUserForm
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import Account, sni, Question, Option
class AccountAdmin(UserAdmin):
list_display = ('userID', 'is_staff', 'is_admin')
search_fields = ['userID']
readonly_fields = ()
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('userID','name','password1','password2','dateOfBirth','homeAddress', 'is_staff', 'is_admin', 'sni'),
}),
)
filter_horizontal = ()
list_filter = ()
fieldsets = ()
ordering = ('userID',)
admin.site.register(Account, AccountAdmin)
admin.site.register(sni)
admin.site.register(Question)
admin.site.register(Option)
Model.py
from asyncio import FastChildWatcher
import email
from pyexpat import model
from xxlimited import Null
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
class userCouncil(BaseUserManager):
def create_user(self, userID, password=None):
if not email:
raise ValueError("Email is required")
user = self.model(userID = self.normalize_email(userID))
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, userID, password):
user = self.model(userID = self.normalize_email(userID))
user.set_password(password)
user.is_staff = True
user.is_admin = True
user.save(using=self._db)
return user
class sni(models.Model):
SNI = models.CharField(max_length=256, primary_key=True)
Used = models.IntegerField(null=True, blank=True)
password = None
USERNAME_FIELD = 'SNI'
def __str__(self):
return self.SNI
class Account(AbstractBaseUser):
userID = models.EmailField(primary_key= True ,max_length=256, unique=True)
name = models.CharField(max_length=100)
dateOfBirth = models.DateField(max_length=8, null=True)
homeAddress = models.CharField(max_length=100, null=True)
is_staff = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
sni = models.OneToOneField(sni, on_delete=models.CASCADE, null=True, blank=True)
USERNAME_FIELD = 'userID'
objects = userCouncil()
def __str__(self):
return self.userID
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(self, app_label):
return True
class Question(models.Model):
QuestionsID = models.IntegerField(primary_key=True, unique=True)
QuestionText = models.CharField(max_length=256, null = True)
class Option(models.Model):
OptionID = models.IntegerField(primary_key=True, unique=True)
OptionText = models.CharField(max_length=256, null=True)
QuestionsID = models.ForeignKey(Question, on_delete=models.CASCADE)
CodePudding user response:
Your Question model needs a def __str__(self) function like your Account model. If you want to represent it with question ID it should look like this:
def __str__(self):
return self.QuestionsID
I don't remember if it will give an error because of it being other data type than string, in that case just cast it to string


