SoFunction
Updated on 2024-10-28

Example of Django --Xadmin Determining the Identity of a Logged-In User

One, in order for xadmin logins to only see the data they create

1,model

class UserTB():
  name=('Name',max_length=30,blank=True,null=True)
  email=('Mailbox',max_length=200,blank=True,null=True)
  integral = ('Points', default=0)
  create_time=('Creation time',auto_now_add=True)
  user = (User,blank=True,null=True) #
  def __unicode__(self):
    return 
  class Meta:
    verbose_name='Employee Management'
    verbose_name_plural='Employee Management'
class UnitPage():
  title=('Title',max_length=50)
  uploader=(UserTB,verbose_name='Creator',related_name='deuser',blank=True,null=True,on_delete=models.SET_NULL)
  create_time=('Creation time',auto_now_add=True)
  def __unicode__(self):
    return 
  class Meta:
    verbose_name='Article Management'
    verbose_name_plural='Article Management'

2,adminx

class ProdeUnitDisplay(object):
  list_display=['title','create_time'] # Need to show fields
  search_fields=['title',] # of searchable fields
  def queryset(self):
    if not .is_superuser: # Determine if you are a super user
      st = (user=) # Find the corresponding user in the user table
      print 'st:',st
      sr = (uploader=st) # Find the data created by the user in the model.
      return sr
 
    return ()
(UnitPage,ProdeUnitDisplay)

Additional knowledge:Django2 Integration xadmin Explained-5-get logged in user information and populate the corresponding Model fields

I. Scenario issues

Continuing with the IDC Model, add the user field to this Model to record the user who added the current data:

class IDC():
  user = (User, on_delete=, editable=False, null=True) # Record the user who created the data
  name = (max_length=64)
  contact = (max_length=32)
  phone = (max_length=32)
  address = (max_length=128)
  create_time = (auto_now=True)

  def __str__(self):
    return 

  class Meta:
    verbose_name = "IDC server room"
    verbose_name_plural = verbose_name

Question: How can I get the currently logged in user information and save it to the user field when adding IDC data?

II. Solutions

Open the IdcManager directory's and add the save_models method to the IDCAdmin class with the following code:

@(IDC)
class IDCAdmin(object):
  list_display = ("user", "name", "contact", "phone", "address", "create_time")
  list_display_links = ("name",)

  def save_models(self):
    self.new_obj.user = 
    super().save_models()

III. Summary

xadmin documentation and information is relatively small, this problem is only three lines of code, but I did not find a solution in Baidu.

It was eventually solved by looking at xadmin's code fiddling around.

So which part of the code should be looked at? Considering that this action is triggered when the data is saved, then look for the xadmin code that handles the save operation (edit screen):

venv\Lib\site-packages\xadmin\views\

Above this Django --Xadmin judgment of the identity of the logger example is all that I have shared with you, I hope to be able to give you a reference, and I hope you support me more.