Django管理过滤器(按国外型号)';由原始sql检索的属性
如何使用属性进行筛选。我知道这是重复的,但我认为有效的现有答案并不适合我 我的Django管理过滤器(按国外型号)';由原始sql检索的属性,django,Django,如何使用属性进行筛选。我知道这是重复的,但我认为有效的现有答案并不适合我 我的模型有这些模型。py: class Profile(models.Model): name = models.CharField(max_length=30, blank=True, null=True) country = models.CharField(max_length=30, blank=True, null=True) class Investment(models.Model):
模型有这些模型。py
:
class Profile(models.Model):
name = models.CharField(max_length=30, blank=True, null=True)
country = models.CharField(max_length=30, blank=True, null=True)
class Investment(models.Model):
...
deal_type = models.CharField(max_length=30, choices=deal_type_choices)
@property
def investor_country(self):
with connection.cursor() as cursor:
# Data retrieval operation - no commit required
cursor.execute('SELECT crowdfunding_profile.country FROM crowdfunding_profile WHERE crowdfunding_profile.associated_entity_id = %s ORDER BY crowdfunding_profile.associated_entity_id DESC LIMIT 1', (int(self.profile.id),))
row = cursor.fetchone()
if not row is None:
return row[0]
return self.profile.country
class InvestmentAdmin(ImportExportModelAdmin, admin.ModelAdmin):
list_display = ('deal_type', 'investor_country')
list_filter = ('deal_type',)
这是我的admin.py上的InvestmentAdmin
:
class Profile(models.Model):
name = models.CharField(max_length=30, blank=True, null=True)
country = models.CharField(max_length=30, blank=True, null=True)
class Investment(models.Model):
...
deal_type = models.CharField(max_length=30, choices=deal_type_choices)
@property
def investor_country(self):
with connection.cursor() as cursor:
# Data retrieval operation - no commit required
cursor.execute('SELECT crowdfunding_profile.country FROM crowdfunding_profile WHERE crowdfunding_profile.associated_entity_id = %s ORDER BY crowdfunding_profile.associated_entity_id DESC LIMIT 1', (int(self.profile.id),))
row = cursor.fetchone()
if not row is None:
return row[0]
return self.profile.country
class InvestmentAdmin(ImportExportModelAdmin, admin.ModelAdmin):
list_display = ('deal_type', 'investor_country')
list_filter = ('deal_type',)
它工作正常,但我找不到将投资者国家/地区添加到列表\u过滤器中的方法。大概是这样的:
list_filter = ('deal_type', 'investor_country')
我试着做一些类似或的事情,因为这是我遇到的最相似的问题:
list_filter = ('deal_type', 'profile__investor_country')
但我收到一个错误:
(models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.