为什么我会收到此错误?
我的Models.py:
# -*- coding: utf-8 -*-
from django.db import models
from sitem.ligler.models import *
from sitem.takimlar.models import *
from sitem.futbolcular.models import *
from sitem.stadyumlar.models import *
from sitem.ligmaclari.models import *
DAKIKA_SECIMLERI = [(str(dk), str(dk)) for dk in range(1, 121)]
KART_SECIMLER = (
('SK','Sari Kart'),
('SKK','Sarıdan Kırmızı Kart'),
('DKK','Direk Kırmızı Kart')
)
# Create your models here.
class EvSahibiKartlar(models.Model):
mac = models.ForeignKey(Ligmaclari)
ev_sahibi_oyuncu = models.ForeignKey(Futbolcular, limit_choices_to = Ligmaclari.ev_sahibi_takim_kadro, related_name='Ev Sahibi Oyuncu')
kart_tipi = models.CharField(max_length=50, choices=KART_SECIMLER)
dakika = models.PositiveSmallIntegerField(max_length=4, choices=DAKIKA_SECIMLERI)
def __unicode__(self):
return self.mac
class KonukKartlar(models.Model):
mac = models.ForeignKey(Ligmaclari)
konuk_takim_oyuncu = models.ForeignKey(Futbolcular, limit_choices_to = Ligmaclari.konuk_takim_kadro, related_name='Konuk Takım Oyuncu')
kart_tipi = models.CharField(max_length=50, choices=KART_SECIMLER)
dakika = models.PositiveSmallIntegerField(max_length=4, choices=DAKIKA_SECIMLERI)
def __unicode__(self):
return self.mac
我的admin.py:
from sitem.kartlar.models import *
from django.contrib import admin
from django import forms
from django.forms import ModelForm
class EvSahibiKartlarAdmin(admin.ModelAdmin):
ordering = ['mac']
class KonukKartlarAdmin(admin.ModelAdmin):
ordering = ['mac']
admin.site.register(EvSahibiKartlar,EvSahibiKartlarAdmin)
admin.site.register(KonukKartlar,KonukKartlarAdmin)
其实我想过滤ForeignKeys ......
答案 0 :(得分:2)
limit_choices_to
未被传递a valid value。
答案 1 :(得分:2)
问题有点老了,但是如果您要进行复杂的查询(例如,嵌套外键过滤),则可以使用类models.Q
并直接分配给limit_choices_to
来自docs
limit_choices_to
是或返回Q对象,这对于 复杂的查询
示例:
from django.db import models
class Model1(models.Model):
name = models.CharField(max_length=255)
is_active = models.BooleanField(default=True)
class Meta:
ordering = ("id",)
class Model2(models.Model):
name = models.CharField(max_length=255)
model_1 = models.ForeignKey(Model1, on_delete=models.CASCADE)
class Meta:
ordering = ("id",)
class Model3(models.Model):
name = models.CharField(max_length=255)
model_2 = models.ForeignKey(Model2, on_delete=models.DO_NOTHING, limit_choices_to=models.Q(model_1__is_active=True))
class Meta:
ordering = ("id",)
答案 2 :(得分:1)
受https://stackoverflow.com/a/26203568/2144569
启发的解决方案您(以及我)在将查询集传递给limit_choices_to时遇到此错误,而是直接传递pk(即:{pk__in:pks}
)并且它将起作用:
我在尝试删除limit_choices_to中的重复时遇到此错误
limit_choices_to = Q(attr1=True) | Q(attr2=True) | Q(attr3=True)
我试着这样做:
def my_func():
return MyModel.objects.filter(Q(attr1=True) | Q(attr2=True) | Q(attr3=True)).distinct().all()
class HeadModel(models.Model):
mtm = models.ManyToManyField(
MyModel,
limit_choices_to=my_func,
)
但我得到了上面的错误,因为它是一个查询集。最后我做了
def my_func():
return {'pk__in':MyModel.objects.filter(Q(attr1=True) | Q(attr2=True) | Q(attr3=True)).distinct().values_list('id', flat=True)}
class HeadModel(models.Model):
mtm = models.ManyToManyField(
MyModel,
limit_choices_to=my_func,
)
P.S:是的,这是一个非常古老的帖子,但需要是/仍然在这里