Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.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 Django多外键集\全部输入模板_Python_Django - Fatal编程技术网

Python Django多外键集\全部输入模板

Python Django多外键集\全部输入模板,python,django,Python,Django,我有两个应用程序。一个customer和另一个xyz。在我的xyz应用程序中,我有4个型号国家、州、城市和商店。在我的客户应用程序中,我有一个型号客户 我的客户模型 class Customers(models.Model): shop = models.ForeignKey(Shops) user = models.ForeignKey(User) uuid = UUIDField(auto=True) name = models.CharField(max_l

我有两个应用程序。一个
customer
和另一个
xyz
。在我的
xyz
应用程序中,我有4个型号<代码>国家、州、城市和商店。在我的客户应用程序中,我有一个型号
客户

我的客户模型

class Customers(models.Model):
    shop = models.ForeignKey(Shops)
    user = models.ForeignKey(User)
    uuid = UUIDField(auto=True)
    name = models.CharField(max_length=200)
    country = models.ForeignKey(Country)
    state = models.ForeignKey(States)
    city = models.ForeignKey(Cities)
我的看法

def edit_customer(request,uuid):
    current_customer = Customers.objects.filter(uuid=uuid,deleted=0)
    return render_to_response('customers/edit_customer.html',{'current_customer':current_customer},context_instance=RequestContext(request))
我试过这个模板。但是,什么也没找到

{% for customer in current_customer %}  

    {% for cn in customer.country_set.all %}
        <li>{{ cn.name }} </li>
        <li>{{ cn.id }} </li>
    {% endfor %}

    {% for st in customer.states_set.all %}
        <li>{{ st.name }} </li>
        <li>{{ st.id }} </li>
    {% endfor %}

    {% for ct in customer.cities_set.all %}
        <li>{{ ct.name }} </li>
        <li>{{ ct.id }} </li>
    {% endfor %}

{% endfor %}
{%用于当前客户中的客户_customer%}
{customer.country\u set.all%中的cn为%1}
  • {{cn.name}
  • {{cn.id}
  • {%endfor%} {customer.states_set.all%}
  • {{st.name}}
  • {{st.id}}
  • {%endfor%} {customer.cities\u set.all%中ct的百分比]
  • {{ct.name}
  • {{ct.id}
  • {%endfor%} {%endfor%}

    正确的方法是什么

    你正试图反过来做。正如django文件所述:

    Django还为关系的“另一方”(从相关模型到定义关系的模型的链接)创建API访问器。例如,博客对象b可以通过Entry_set属性访问所有相关条目对象的列表:b.Entry_set.all()

    请注意
    另一面

    因此,在您的情况下,您可以执行
    state.customer\u set.all()
    。但是,看看您试图实现的目标,我猜您使用了错误的字段类型,如果您希望
    客户
    能够选择多个位置,您应该使用
    ManyToManyField

    class Customer(models.Model):
        shop = models.ForeignKey(Shops)
        user = models.ForeignKey(User)
        uuid = UUIDField(auto=True)
        name = models.CharField(max_length=200)
        countries = models.ManyToManyField(Country)
        states = models.ManyToManyField(States)
        cities = models.ManyToManyField(Cities)
    
    然后您可以执行类似于
    customer.countries.all()
    customer.states.all()
    customer.cities.all()

    更新:将数据添加到
    ManyToManyField

    要将数据添加到
    ManyToManyField
    ,可以执行以下操作:

    customer = Customer(shop_id=shop_id, name=name)
    customer.save()
    
    # Add ManyToMany's
    custome.states.add(state)
    customer.cities.add(cities)
    customer.countries.add(country)
    

    我还建议您阅读django文档,了解我目前正在插入的客户数据。现在我把我的领域换成了很多。所以我不能像这样插入countr、state和city。如何在customers\u countries表中插入客户数据。c=客户(店铺id=店铺id,姓名=姓名,国家=国家,州=州,城市=城市)c.保存()