我在我的应用程序中使用django+jquery自动完成小部件。我定制了一个表的管理表单,以便在输入文本框中自动完成。它正在工作,只是当我保存表单时,会出现以下异常:
ValueError at /admin/Stock/venta/add/
Cannot assign "u'Agua'": "Venta.producto" must be a "Producto" instance.
Request Method: POST
Request URL: http://127.0.0.1:8080/admin/Stock/venta/add/
Exception Type: ValueError
Exception Value:
Cannot assign "u'Agua'": "Venta.producto" must be a "Producto" instance.
Exception Location: /usr/lib/pymodules/python2.6/django/db/models/fields/related.py in __set__, line 273
Python Executable: /usr/bin/python
Python Version: 2.6.5
...
它似乎没有将Producto对象中的自动完成文本转换。我看到了帖子,它正在发送所选产品的数字键(即: 2)。当我禁用所有的自动完成的东西,文章是完全一样的,但它的工作。因此,admin.py或models.py源代码是错误的。在这种情况下,它会把它转换成一个对象,而不是另一个对象。
以下是models.py部分:
class Producto(models.Model):
detalle = models.CharField('Detalle', max_length=200)
importe = models.FloatField('Importe')
def __unicode__(self):
return self.detalle
class Empleado(models.Model):
nombre = models.CharField('Nombre', max_length=100)
def __unicode__(self):
return self.nombre
class Venta(models.Model):
importe = models.FloatField('Importe')
producto = models.ForeignKey(Producto)
responsable = models.ForeignKey(Empleado)
mesa = models.IntegerField()
以下是admin.py部分:
class VentaAdminForm(forms.ModelForm):
importe = forms.DecimalField()
producto = forms.CharField()
responsable = forms.CharField()
mesa = forms.IntegerField()
class Meta:
model = Venta
fields = ['producto', 'importe', 'responsable', 'mesa']
class VentaAdmin(admin.ModelAdmin):
form = VentaAdminForm
admin.site.register(Venta, VentaAdmin)
views.py
@login_required
def search(request):
results = []
if request.method != "GET":
return HttpResponse()
term = q = None
if request.GET.has_key(u'q'):
q = request.GET[u'q']
if request.GET.has_key(u'term'):
term = request.GET[u'term']
if not q or not term:
return HttpResponse()
if q == 'producto':
model_results = Producto.objects.filter(detalle__contains=term)
for x in model_results:
results.append({'label': x.detalle,'value': x.detalle, 'id': x.id })
elif q == 'responsable':
model_results = Empleado.objects.filter(nombre__contains=term)
for x in model_results:
results.append({'label': x.nombre,'value': x.nombre, 'id': x.id })
else:
raise Exception("Unknown query_object")
json = simplejson.dumps(results)
return HttpResponse(json, mimetype='application/json')
javascript部分:
<script>
$(function() {
$( "#id_producto" ).autocomplete({
source: "/search/?q=producto",
});
$( "#id_responsable" ).autocomplete({
source: "/search/?q=responsable",
});
});
</script>
当自动完成文本框中的写(即: agua )发送GET时,响应如下。
http://127.0.0.1:8080/search/?q=producto&term=agua
[{"id": 3, "value": "Agua", "label": "Agua"}]
版本
django 1.1.1
jquery 1.5.1
jquery-ui 1.8.13
发布于 2011-07-09 03:57:56
它看起来像是"Agua“是被传递回控制器的值,而Rails则期望您传递"3”。你能试着改变你的后端
[{"value": "3", "label": "Agua"}]
看看它是否有效
发布于 2011-07-09 09:41:09
producto = models.ForeignKey(Producto)
在Venta模型定义中,您将producto定义为foreignkey,这意味着当您保存表单时,django 希望获取相关对象的id (在这种情况下,相关产品的id为记录),而不是记录的标签。
Django使用组合框来处理这些外键,其html输出如下:
<select name='producto'>
<option value='3'>Agua</option>
...
如果将该字段显示为raw_id_field (文档),django将显示对象的id,并在字段附近写入unicode值。
因此,您必须传递--关联对象的id,而不是名称或unicode字符串。我不使用自动完成小部件,但是必须有正确的方法来正确地完成它。
https://stackoverflow.com/questions/6631910
复制相似问题