Django - 在render_change_form(ModelAdmin)中获取对象标识 [英] Django - Get object id in render_change_form (ModelAdmin)

查看:602
本文介绍了Django - 在render_change_form(ModelAdmin)中获取对象标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这两个模型和modeladmin。在可用hostuser列表中添加新主机时,只会显示未分配给其他主机的主机。
问题是如果我编辑已经创建的主机,它的实际hostuser id也被过滤,所以我想做的是排除当前分配的hostuser id。
我可以在hostuser字段中排除当前的id吗?



我需要的语句是写在*



提前感谢



Models.py

  class HostUser(models.Model):
name = models.CharField(max_length = 200)
{..More Data ..}

class主机(型号。模型):
{..More Data ..}
hostuser = models.ForeignKey(HostUser,blank = True,null = True)

Admin.py

  class HostAdmin(admin.ModelAdmin):
{..More Data ..}
def render_change_form(self,request,context,* args,** kwargs):
list_names = Host.objects.values_list('hostuser__id',flat = true).exclude(hostuser__id = None).exclude(hostuser__id = ** ACTUAL HOSTUSER_ID **)
list_names = [int(ids)for id_number]
context ['adminform']。 fields ['hostuser']。queryset = HostUser.objects.exclude(id__in = lis t_names)
return super(HostAdmin,self).render_change_form(request,context,args,kwargs)


解决方案

(在编辑问题中回答。转换为社区维基答案。请参阅当问题的答案被添加到问题本身时,什么是适当的行动?



OP写道:


使用kwargs解决,Modeladmin如下所示:

  def render_change_form(self,request,context,* args,** kwargs):
try:
list_names = Host.objects.values_list('hostuser__id',flat = True).exclude(hostuser__id = None ).exclude(hostuser__id = kwargs ['obj']。hostuser.id)
除了:
list_names = Host.objects.values_list('hostuser__id',flat = True).exclude(hostuser__id = None)
list_names = [int(id)ids在list_names]
上下文['adminform']。form.fields ['hostuser']。queryset = HostUser.objects.exclude(id__in = list_names)
return super(HostAdmin,self).render_change_form(request,co ntext,args,kwargs)



I have these two models and modeladmin. When adding a new host in the list of available hostuser only appear hostusers that are not assigned to another host. The issue is if I edit an already created host its actual hostuser id is also filtered so I want to do is to exclude hostuser id that is currently assigned. How I can specify in the exclude the current id from the hostuser field?

The statement that I need is written between *

Thanks in advance

Models.py

class HostUser(models.Model):
    name = models.CharField(max_length=200)
    {..More Data..}

class Host(models.Model):
    {..More Data..}
    hostuser = models.ForeignKey(HostUser, blank=True, null=True)

Admin.py

class HostAdmin(admin.ModelAdmin):
    {..More Data..}
    def render_change_form(self, request, context, *args, **kwargs):
        list_names = Host.objects.values_list('hostuser__id', flat=True).exclude(hostuser__id=None).exclude(hostuser__id=**ACTUAL HOSTUSER_ID**)
        list_names = [int(ids) for ids in list_names]
        context['adminform'].form.fields['hostuser'].queryset = HostUser.objects.exclude(id__in=list_names)
        return super(HostAdmin, self).render_change_form(request, context, args, kwargs)

解决方案

(Answered in a question edit. Converted to a community wiki answer. See What is the appropriate action when the answer to a question is added to the question itself? )

The OP wrote:

Solved using kwargs, the Modeladmin looks like this:

def render_change_form(self, request, context, *args, **kwargs):
    try:
        list_names = Host.objects.values_list('hostuser__id', flat=True).exclude(hostuser__id=None).exclude(hostuser__id=kwargs['obj'].hostuser.id)
    except:
        list_names = Host.objects.values_list('hostuser__id', flat=True).exclude(hostuser__id=None)
    list_names = [int(ids) for ids in list_names]
    context['adminform'].form.fields['hostuser'].queryset = HostUser.objects.exclude(id__in=list_names)
    return super(HostAdmin, self).render_change_form(request, context, args, kwargs)

这篇关于Django - 在render_change_form(ModelAdmin)中获取对象标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆