Walf Sun
Walf Sun

Reputation: 1

I have a structural problem with my user management code in django

I'm new to django. I'm trying to make a custom user management in django admin.

I have an admin,py like this.

""" from django.contrib import admin from django.contrib.auth.admin import UserAdmin as BaseUserAdmin from .forms0 import UserCreationForm, UserAdminChangeForm from .models import User #from django.contrib import apps.authentication.models import apps.site.register(model)

Register your models here.

class UserAdmin(BaseUserAdmin):
    search_fields = ['employee', 'username']

    add_form = UserAdminCreationForm
    form = UserAdminChangeForm

    list_display = ('username', 'employee', 'admin', 'staff', 'active')
    list_filter = ('admin', 'staff', 'active')

"""

It calls form0.py first before my model.py

""" add_form = UserAdminCreationForm form = UserAdminChangeForm """

Models.py

""" from django.db import models from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin, UserManager

class User(AbstractBaseUser, PermissionsMixin):
    username = models.CharField(max_length=255, unique=True, null=False, blank=False, verbose_name='Username')
    employee = models.OneToOneField('Employee',
                                    null=True,
                                    blank=True,
                                    verbose_name='Employee ID',
                                    related_name='user',
                                    on_delete=models.PROTECT)
    admin = models.BooleanField(default=False)
    active = models.BooleanField(default=True)
    staff = models.BooleanField(default=False)
    timestamp = models.DateTimeField(auto_now_add=True)

"""

form0.py

""" from django import forms from django.db import models from .models import User

class UserAdminCreationForm(forms.ModelForm):
    """A form for creating new users. Includes all the required
    fields, plus a repeated password."""

    password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
    password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ('username', 'employee', 'admin', 'active', 'staff', 'groups')

"""

I get this error When I migrate it loads forms0 first before model and I get the following

'ValueError: Cannot create form field for 'employee' yet, because its related model 'Employee' has not been loaded yet'

I have a conflict if I refer to admin.py. It gives me this error.

"ImportError: cannot import name 'employee' from partially initialized module 'apps.main.admin' (most likely due to a circular import)"

Please let me know how I can structure my code to avoid this conflict.

Upvotes: 0

Views: 48

Answers (1)

KamilBaranowskiCZ
KamilBaranowskiCZ

Reputation: 26

You should use Employee, not the string 'Employee'

employee = models.OneToOneField('Employee', /// this shouldn't be string. Try just Employee without ''
                                    null=True,
                                    blank=True,

Upvotes: 0

Related Questions