I have three models, ‘Clientes’ (Clients), ‘Pedidos’ (Orders) and ‘DireccionEntrega’ (DeliveryAddress).In clients I defined a field to be the client id ‘id_cliente’ and which is a ForeignKey in the DeliveryAddress model. Each client can have different delivery addresses. When an order is placed, the employee should be able to create an Order that has the customer id and its delivery address. Now the problem is that many fields in the DeliveryAdress model make up one address, i.e. ‘address’, ‘city’, ‘zip_code’, ‘country’, and I just care about saving the entire DeliveryAdress an Order as a single string in a field that only allows to choose between the Delivery Addresses (direccion_entrega) a Client has. Here are some bits of my code for reference:
class Clientes(models.Model):
id_cliente = models.CharField(
'Codigo de Cliente', primary_key=True, max_length=6
)
#...
class DireccionEntrega(models.Model):
id_cliente = models.ForeignKey(
Clientes, db_column='id_cliente', default=None,
on_delete=models.CASCADE, verbose_name='Codigo de Cliente'
)
direccion = models.CharField(
'Direccion', max_length=100, null=False, blank=False
)
distrito = models.CharField(
'Distrito', max_length=100, null=False, blank=False
)
codigo_postal = models.CharField(
'Codigo Postal', max_length=10, null=False, blank=False, default=None
)
ciudad = models.CharField(
'Ciudad', max_length=100, null=False, blank=False
)
pais = models.CharField(
'Pais',
max_length=100,
choices=Categoria.pais(),
default=None,
)
#...
class Pedidos(models.Model):
id_pedido = models.CharField(
'Codigo de Pedido', primary_key=True, max_length=6
)
id_cliente = models.ForeignKey(
Clientes, db_column='id_cliente', default=None,
on_delete=models.CASCADE, verbose_name='Codigo de Cliente'
)
id_vendedor = models.ForeignKey(
Vendedores, db_column='id_vendedor', default=None,
on_delete=models.CASCADE, verbose_name='Codigo de Vendedor'
)
fechaPedido = models.DateTimeField(
auto_now_add=True
)
## IN THE FOLLOWING FIELD IS WHERE IM TRYING TO SAVE THE DELIVERY ADDRESS
direccion_entrega = ForeignKey(
DireccionEntrega, db_column='direccion'
)
#...