Hi,

I'm trying to create a formset for a model that has multiple foreign keys 
and many to many fields.

The goal is the be able to find an item in a pre-populated list of 
Equipment and Material, select multiple items and add/change the quantity 
of each item, inside the formset.

I realize that I haven't added the quantity fields in the JobDocket, but I 
have no idea how to define that kind of relationship.

Models:

class Equipment(models.Model):
    equipment_name = models.CharField(max_length=30)
    supplier = models.CharField(max_length=50, blank=True)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    quantity = models.IntegerField(null=True)
    margin = models.DecimalField(max_digits=3, decimal_places=2, 
help_text='Input as a decimal e.g: 75% as 0.75')
    photo = models.ImageField()

    def __str__(self):
        return self.equipment_name


class Material(models.Model):
    material_name = models.CharField(max_length=30)
    supplier = models.CharField(max_length=50, blank=True)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    margin = models.DecimalField(max_digits=3, decimal_places=2, 
help_text='Input as a decimal e.g: 75% as 0.75')
    quantity = models.IntegerField()
    photo = models.ImageField()

    def __str__(self):
        return self.material_name


class JobDocket(models.Model):
    EQUIPMENT_TYPE = (
        ('1', 'Air Conditioning'),
        ('2', 'Heating/Heat-Pump'),
        ('3', 'Refrigeration'),
        ('4', 'Ventilation/Extraction'),
        ('5', 'Electrical/Controls'),
    )

    job = models.ForeignKey(Job, on_delete=models.CASCADE)
    technician = models.ForeignKey(User, on_delete=models.CASCADE)
    site_equipment = models.CharField(choices=EQUIPMENT_TYPE, max_length=50, 
default='1')
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
    rate = models.DecimalField(max_digits=20, decimal_places=2, default=80.00)
    mileage = models.IntegerField()
    equipment_used = models.ManyToManyField(Equipment, max_length=500)
    materials_used = models.ManyToManyField(Material, max_length=500)
    description = models.CharField(max_length=1000, help_text='Description of 
work carried out', blank=False)
    created = models.DateTimeField(default=now, blank=True)

    def __str__(self):
        return str(self.technician)

    def get_absolute_url(self):
        return reverse('jobs:detail', kwargs={'pk': self.pk})



Could someone show me how to define this relationship as well as how to 
create the formset for it?

Thanks,
Sam


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/00c547b6-b626-4ea9-b4f3-3ed94445060d%40googlegroups.com.

Reply via email to