我试图建立一个广告资源面板, 无法在admin.py中的IssuedAdmin中运行save_model()
我试图获取发布该商品的人的用户名
在InventoryIssued类中:
models.py
class InventoryIssued(models.Model):
"""Inventory Issued to person """
get_user_type = models.ForeignKey(RoleOfUser, on_delete=models.CASCADE)
user = models.ForeignKey(settings.AUTH_USER_MODEL,
null=True, blank=True,
on_delete=models.SET_NULL,
)
get_users = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name = 'issued')
item_name = models.ManyToManyField(InventoryPresent)
date = models.DateField(auto_now_add = True)
quantity = models.IntegerField(default = '1')
status = [("APP","Approved"),
("DEC","Declined"),]
item_status = models.CharField(choices = status, blank=False, max_length = 3)
def __str__(self):
return self.get_users
class Meta:
verbose_name_plural = 'Inventory Issued'
class User_manager(BaseUserManager):
def create_user(self, username, email, name,
phone_number,password):
email = self.normalize_email(email)
user = self.model(username=username, email=email, name=name,
phone_number=phone_number,)
user.is_active = True
user.set_password(password)
user.save(using=self.db)
return user
def create_superuser(self, username, email, name,
phone_number,password):
user = self.create_user(username=username, email=email, name=name,
phone_number=phone_number, password=password)
user.is_superuser = True
user.is_staff = True
user.save(using=self._db)
return user
class UserProfile(PermissionsMixin, AbstractBaseUser):
username = models.CharField(max_length=32, unique=True,)
email = models.EmailField(max_length=32, unique=True,)
name = models.CharField(max_length=32, blank=False,)
user_type = models.ForeignKey(
RoleOfUser,
on_delete=models.CASCADE,
null=True, blank=True)
phone_number = PhoneField(unique=True,
blank=False,)
date_created = models.DateField(auto_now_add = True)
is_active = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
REQUIRED_FIELDS = ["email","name",
"phone_number",]
USERNAME_FIELD = "username"
objects = User_manager()
def get_full_name(self):
""" Retrive full Name """
return self.name
def __str__(self):
return str(self.username)
admin.py
@admin.register(InventoryIssued)
class IssuedAdmin(admin.ModelAdmin):
readonly_fields = ['user',]
fields = (
('get_user_type', 'get_users'),
('item_name', 'quantity'),
('item_status','user')
)
def save_model(self, request, obj, form, change):
pdb.set_trace()
obj.user = request.user
obj.save()
# super().save_model(request, obj, form, change)
@admin.register(UserProfile)
class UserProfileAdmin(admin.ModelAdmin):
fields = (
('username', 'email'),
('name','user_type'),
('phone_number'),
('is_active','is_superuser','is_staff')
)
输出
-> obj.user = request.user
(Pdb) c
Internal Server Error: /admin/users/inventoryissued/add/
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/options.py", line 606, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/sites.py", line 223, in inner
return view(request, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/options.py", line 1634, in add_view
return self.changeform_view(request, None, form_url, extra_context)
File "/usr/local/lib/python3.7/site-packages/django/utils/decorators.py", line 45, in _wrapper
return bound_method(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/options.py", line 1522, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/options.py", line 1565, in _changeform_view
self.log_addition(request, new_object, change_message)
File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/options.py", line 810, in log_addition
object_repr=str(object),
TypeError: __str__ returned non-string (type UserProfile)
[30/Dec/2019 19:08:54] "POST /admin/users/inventoryissued/add/ HTTP/1.1" 500 111083
答案 0 :(得分:0)
您的component
是用户模型的get_user
。
如果您这样定义:
ForeignKey
您不返回一个字符串(这对于def __str__(self):
return
方法是必需的),但返回一个self.get_user__str__
对象。不过,您可以在其上调用UserProfile
,以将其转换为字符串:
str(..)