Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 两个模型。选择列表。不是唯一的_Python_Django - Fatal编程技术网

Python 两个模型。选择列表。不是唯一的

Python 两个模型。选择列表。不是唯一的,python,django,Python,Django,我有两个模型。我希望第一个模型字段是第二个模型字段的选择列表。但二次模型的研究领域并不是唯一的 想一想,需要在管理表单中重新定义精细字段 请帮我认识到这一点。我在学习django country models.py from django.db import models from django.contrib.auth.models import User class Country(models.Model): class Meta(): db_table = 'c

我有两个模型。我希望第一个模型字段是第二个模型字段的选择列表。但二次模型的研究领域并不是唯一的

想一想,需要在管理表单中重新定义精细字段

请帮我认识到这一点。我在学习django

country models.py

from django.db import models
from django.contrib.auth.models import User

class Country(models.Model):
    class Meta():
        db_table = 'country'

    country_region = models.CharField(max_length=300)
    country_city = models.CharField(max_length=300)
    country_district = models.CharField(max_length=300, blank=True, null=True)
from django.db import models
from django.contrib.auth.models import User
from client.models import Client

# Create your models here.
class Client(models.Model):
    class Meta():
        db_table = 'client'

    client_name = models.CharField(max_length=300)
    client_region = models.CharField(max_length=300) #maybe ForeignKey
客户端模型.py

from django.db import models
from django.contrib.auth.models import User

class Country(models.Model):
    class Meta():
        db_table = 'country'

    country_region = models.CharField(max_length=300)
    country_city = models.CharField(max_length=300)
    country_district = models.CharField(max_length=300, blank=True, null=True)
from django.db import models
from django.contrib.auth.models import User
from client.models import Client

# Create your models here.
class Client(models.Model):
    class Meta():
        db_table = 'client'

    client_name = models.CharField(max_length=300)
    client_region = models.CharField(max_length=300) #maybe ForeignKey
我想选择列表(选项)在客户端_地区(从国家地区)在管理

我正在试着用admin.py

from django.contrib import admin
from country.models import Country
from client.models import Client

class ClientAdmin(admin.ModelAdmin):

    def __init__(self, *args, **kwargs):
            new_choices = Country.objects.values('country_region').distinct()
            super(ClientAdmin.form, self).__init__(*args, **kwargs)
            self.field['client_region'].choices = new_choices

# Register your models here.
admin.site.register(Client, ClientAdmin)
我尝试了这个代码,但它不起作用

管理员

from django.contrib import admin
from timetable.models import Client
from house.models import Country
from django.forms import ModelChoiceField, ModelForm, forms

class ClientForm(ModelForm):
    class Meta:
        model = Client

    def __init__(self, *args, **kwargs):
        super(ClientForm, self).__init__(*args, **kwargs)
        self.field['client_region'] = forms.ModelChoiceField(queryset=Country.objects.values['county_region'])

class ClientAdmin(admin.ModelAdmin):
    list_display = ['client_region']
    form = ClientForm

# Register your models here.
admin.site.register(Client, ClientAdmin)
对于您想要做的事情(如果我理解,请按地区获取客户列表),我的方法是创建第三个模型“region”,并使用ForeignKey将客户和国家链接到此模型


不过,我不确定是否能很好地理解您的问题,您能尝试重新表述它吗?

是的,这是一个简单而好的方法。但我想从外地国家/地区列出选择清单。也许试着看看这个威胁: