How to create an element that contains one or more elements from different Tables in my Django database

0

I have the following Tables: Ticket , Manager and Services

Ticket has information such as creation date, manager, services and more. manager that has 2 fields name and percentage. and the services table that has 2 services and price fields.

I want to create a view where I can enter the ticket data and add one or more services on the same ticket.

I already have the form to introduce responsible and services, I have the view that shows the tickets that are in the database  What I have no idea is how to make a ticket have one or more services when creating the ticket

my models.py

from django.db import models
from django.core.urlresolvers import reverse

# Create your models here.
class Servicios(models.Model):
    servicio = models.CharField(max_length=250)
    precio = models.FloatField()


class Encargado(models.Model):
    nombre = models.CharField(max_length=250)
    porcentaje = models.FloatField()

class Ticket(models.Model):
    fecha = models.DateTimeField(auto_now_add=True)
    servicio = models.ForeignKey(Servicios)
    encargado = models.ForeignKey(Encargado)
    cantidad = models.IntegerField()
    p_unitario = models.FloatField()
    total = models.FloatField()


    def get_absolute_url(self):
        return reverse('Main:get-list')
    
asked by victorR 15.10.2017 в 23:52
source

1 answer

0

Like @toledano said if you want a ticket to have one or more than one service and that a service has multiple tickets you can use manytomany it would be something like:

class Ticket(models.Model):
    fecha = models.DateTimeField(auto_now_add=True)
    servicio = models.ManyToManyField(Servicios)
    encargado = models.ForeignKey(Encargado)
    cantidad = models.IntegerField()
    p_unitario = models.FloatField()
    total = models.FloatField()

or you can apply the foreign key in the model Servicios and a ticket would have several services but a service would have only one ticket

    
answered by 16.10.2017 / 23:42
source