我是Django的新手。我想尝试这个应用django-smart-selects。我希望通过在管理网站的“订单”表单中选择合适的“客户”来自动填充“客户”地址。
所以我定义了以下内容
models.py
from smart_selects.db_fields import ChainedForeignKey
from django.db import models
# Adress model
class Adresse (models.Model):
strasse = models.CharField(max_length=50)
plz = models.IntegerField(max_length=5)
ort = models.CharField(max_length=50)
def __unicode__(self):
return self.strasse
# Customer model
class Customer (models.Model):
name = models.CharField(max_length=50)
adresse = models.ForeignKey(Adresse)
def __unicode__(self):
return self.name
# Order model
class Order (models.Model):
name = models.CharField(max_length=50)
delivery_time = models.DateTimeField()
created_at = models.DateTimeField(auto_now_add=True)
kunde = models.ForeignKey(Customer, related_name="Kunde")
adresse = ChainedForeignKey(
Customer,
chained_field="kunde",
chained_model_field="strasse",
show_all=False,
auto_choose=True
)
class Meta:
db_table = 'order'
ordering = ['-created_at']
def __unicode__(self):
return self.name
admin.py
from django.contrib import admin
from customer_order.test_app.models import Adresse, Customer, Order
class AdresseAdmin(admin.ModelAdmin):
list_display = ('strasse', 'plz', 'ort',)
class CustomerAdmin(admin.ModelAdmin):
list_display = ('name', 'adresse',)
list_per_page = 50
class OrdersAdmin(admin.ModelAdmin):
list_display = ('name', 'delivery_time', 'created_at', 'kunde', 'adresse',)
list_display_links = ('name',)
list_per_page = 50
ordering = ['-created_at']
admin.site.register(Adresse, AdresseAdmin)
admin.site.register(Customer, CustomerAdmin)
admin.site.register(Order, OrdersAdmin)
现在,当我想通过admin add-Button将第一个条目添加到“Order”表中时,我收到以下错误:
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/test_app/order/add/
Exception Type: TemplateSyntaxError
Exception Value:
Caught an exception while rendering: Reverse for 'chained_filter' with arguments '()' and keyword arguments '{'field': 'strasse', 'app': 'test_app', 'model': 'Customer', 'value': '1'}' not found.
Original Traceback (most recent call last):
File "C:\Python25\lib\site-packages\django\template\debug.py", line 71, in render_node
result = node.render(context)
File "C:\Python25\lib\site-packages\django\template\debug.py", line 87, in render
output = force_unicode(self.filter_expression.resolve(context))
File "C:\Python25\lib\site-packages\django\utils\encoding.py", line 71, in force_unicode
s = unicode(s)
File "C:\Python25\lib\site-packages\django\forms\forms.py", line 356, in __unicode__
return self.as_widget()
File "C:\Python25\lib\site-packages\django\forms\forms.py", line 391, in as_widget
return widget.render(name, data, attrs=attrs)
File "C:\Python25\lib\site-packages\django\contrib\admin\widgets.py", line 231, in render
output = [self.widget.render(name, value, *args, **kwargs)]
File "H:\customer_order\smart_selects\widgets.py", line 36, in render
url = "/".join(reverse("chained_filter", kwargs={'app':self.app_name,'model':self.model_name,'field':self.model_field,'value':"1"}).split("/")[:-2])
File "C:\Python25\lib\site-packages\django\core\urlresolvers.py", line 350, in reverse
*args, **kwargs)))
File "C:\Python25\lib\site-packages\django\core\urlresolvers.py", line 300, in reverse
"arguments '%s' not found." % (lookup_view_s, args, kwargs))
NoReverseMatch: Reverse for 'chained_filter' with arguments '()' and keyword arguments '{'field': 'strasse', 'app': 'test_app', 'model': 'Customer', 'value': '1'}' not found.
Exception Location: C:\Python25\lib\site-packages\django\template\debug.py in render_node, line 81
Python Executable: C:\Python25\python.exe
Python Version: 2.5.0
答案 0 :(得分:11)
您是否已完成安装说明中的第2步 - 将以下内容添加到您的urls.py中?
url(r'^chaining/', include('smart_selects.urls')),