Detailed -5- Django2 integrated xadmin for login user information and populate the fields Model

A scene problems
continue to use IDC Model, The Model field increases user, add the user to record the current data:

class IDC(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, editable=False, null=True) # 记录创建该数据的用户
name = models.CharField(max_length=64)
contact = models.CharField(max_length=32)
phone = models.CharField(max_length=32)
address = models.CharField(max_length=128)
create_time = models.DateField(auto_now=True)

def __str__(self):
return self.name

class Meta:
verbose_name = "IDC room"
verbose_name_plural = verbose_name
1
2
3
4
5
6
7
8
9
10
11
12
13
14
question: how to increase the IDC data, when obtaining user information currently logged in, and saved to the user field?

Two solutions
open adminx.py IdcManager directory, increase save_models method IDCAdmin class, as follows:

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

save_models DEF (Self):
self.new_obj.user = self.request.user
Super () save_models ().
1
2
3
4
5
6
7
8
III summarizes the
documentation and information xadmin is relatively small, although this issue also three lines of code thing, but I did not find a solution in Baidu.

The final code is to look xadmin fumble resolved.

Well, you should see which parts of the code? Taking into account this operation is triggered when saving data, then save operation to find xadmin processing (editing interface) code:

Venv \ Lib \ Site-Packages Standard Package \ xadmin \ views \ edit.py
----------------
Disclaimer: This article is CSDN blogger "yuhan963 'original article, follow the CC 4.0 BY -SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/yuhan963/article/details/79187425

Guess you like

Origin www.cnblogs.com/skbarcode/p/12584312.html