开发者

_filter_or_exclude() argument after ** must be a mapping, not ReverseManyRelatedObjectsDescriptor

Why i get this error?

My 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ı K开发者_如何学运维art')
)
# 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

My 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)

Actually i want to do filter ForeignKeys...


limit_choices_to isn't being passed a valid value.


Solution inspired by https://stackoverflow.com/a/26203568/2144569

You (and also I) got this error while passing a queryset to limit_choices_to, instead directly pass the pk (i.e:{pk__in:pks}) and it will work :

I was getting this error while trying to remove duplicate in limit_choices_to as I was doing

limit_choices_to = Q(attr1=True) | Q(attr2=True) | Q(attr3=True)

I tried to do :

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,
    )

but I got the error above as it is a queryset. Finally I did

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: yes, it is a very old post, but the need is/was still here


Question is a little bit old, but if you want to do a complex query( eg: nested foreign key filtering ) you can use class models.Q and directly assign to limit_choices_to

from docs

limit_choices_to is or returns a Q object, which is useful for complex queries

Example :

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",)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜