I have a CustomUser
model, and a Retailer
model that holds the additional details of retailer user type. The Retailer
model has a OneToOne relation to CustomUser
model. There is no public user registration or signup, accounts are created by superuser.
In the Django admin site, I am trying to leverage admin.StackedInline in the retailer admin page to enable superusers to create new retailer users directly from the retailer admin page. This eliminates the need to create a new user object separately in the CustomUser
model admin page and then associate it with a retailer object using the default dropdown in the retailer model admin page.
However, I got the below error:
MODELS.PY
class CustomUser(AbstractUser):
"""
Extended custom user model.
"""
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
username = None # type: ignore
first_name = None # type: ignore
last_name = None # type: ignore
name = models.CharField(_("Name of User"), max_length=150)
email = models.EmailField(_("Email address of User"), unique=True, blank=False)
date_modified = models.DateTimeField(auto_now=True)
# Flags for user types
is_retailer = models.BooleanField(
_("Retailer status"),
default=False,
help_text=_("Designates whether the user should treated as retailer"),
)
is_shop_owner = models.BooleanField(
_("Shop owner status"),
default=False,
help_text=_("Designates whether the user should treated as shop owner"),
)
USERNAME_FIELD = "email"
REQUIRED_FIELDS = ["name"]
class Retailer(BaseModelMixin):
"""
Define retailer's profile.
"""
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="retailer")
phone = models.PositiveBigIntegerField(default=0, blank=True)
address = models.TextField(max_length=512, default="", blank=True)
google_map = models.URLField(max_length=1024, default="", blank=True)
slug = AutoSlugField(populate_from="user__name")
def __str__(self):
return self.user.name
ADMIN.PY
class UserInline(admin.StackedInline):
model = User
fields = ["name", "email", "password1", "password2"]
fk_name = "user"
extra = 0
@admin.register(Retailer)
class RetailerAdmin(BaseAdmin):
"""
Admin interface for the Retailer model
This class is inherited from the BaseAdmin class to include common fields.
"""
inlines = [UserInline]
fieldsets = (
(
None,
{"fields": ("user", "phone", "address", "google_map")},
),
)
list_display = [
"user",
"phone",
"created_at",
"updated_at",
]
Edit
I changed the fk_name
attribute of UserInline
class to "retailer", but I'm still getting an error saying 'accounts.CustomUser' has no field named 'retailer'. Where did I go wrong or am I missing something?