gpt4 book ai didi

django - 如何给 `def formfield_for_manytomany` 一个可编辑对象的 id

转载 作者:行者123 更新时间:2023-12-04 22:08:02 26 4
gpt4 key购买 nike

class Report(models.Model):
precursor = models.ManyToManyField(Precursor)

class ReportAdmin(admin.ModelAdmin):
def formfield_for_manytomany(self, db_field, request, **kwargs):
ops_area = Report.objects.get(id=???????).ops_area
print id
print ops_area
if db_field.name == "r_precursor":
kwargs["queryset"] = Precursor.objects.filter(ops_area=ops_area)
print kwargs
return super(ReportAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)

怎么给 def formfield_for_manytomany可编辑对象的 id?

最佳答案

假设您在追求 Report ReportAdmin 正在编辑的实例, 尝试这个:

class ReportAdmin(admin.ModelAdmin):
def get_object(self, request, object_id):
# Hook obj for use in formfield_for_manytomany
self.obj = super(ReportAdmin, self).get_object(request, object_id)
print "Got object:", self.obj
return self.obj

def formfield_for_manytomany(self, db_field, request, **kwargs):
if db_field.name == "r_precursor" and getattr(self, 'obj', None):
kwargs["queryset"] = Precursor.objects.filter(
ops_area=self.obj.ops_area)
return super(ReportAdmin, self).formfield_for_manytomany(
db_field, request, **kwargs)

关于django - 如何给 `def formfield_for_manytomany` 一个可编辑对象的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5213612/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com