【发布时间】:2018-04-20 20:51:46
【问题描述】:
我有 2 个问题。我有 2 个模型 Toolcart 和 Tools 我正在建立从 Toolcart 到 Tools 的 ManyToMany 关系,我想将一个或多个工具添加到购物车
问题 1.- 这是我尝试做的正确方法吗?
问题 2.- 我使用了一个通用类来创建 Toolcart 对象 (CreateView) 并从 Tools 模型中获取一个包含所有内容的框。我将如何制作可选项目。我正在尝试做的事情有意义吗?
我的模型.py:
class Tools(models.Model):
nombre = models.CharField(max_length=250)
descripcion = models.CharField(max_length=250)
codigo_proveedor = models.CharField(max_length=250)
categoria = models.ForeignKey('Categorias', on_delete=models.CASCADE)
c_minima = models.IntegerField()
c_actual = models.IntegerField()
proveedor = models.ForeignKey('Proveedores', on_delete=models.CASCADE)
active = models.BooleanField()
def __str__(self):
return self.nombre + ' ----- ' + str(self.categoria) + ' ----- ' + str(self.c_actual)
class Toolcart(models.Model):
designacion = models.CharField(max_length=250)
contenido = models.ManyToManyField(Tools)
active = models.BooleanField()
def __str__(self):
return self.designacion
我的意见.py:
# VER CARRITO
class CarritoCreateView(CreateView):
model = Toolcart
template_name = 'inventory/cars_form.html'
fields = [
'designacion',
'contenido',
'active',
]
success_url = reverse_lazy('inventory:home')
【问题讨论】:
-
您的问题解决了吗?我下面的回答对你有用吗?
-
您好,感谢您的跟进。老实说,还没有时间实施它。一旦有机会,我会尝试一下,并告诉您何时使用。
标签: django django-models django-forms django-templates django-views