P
P
paevlk20072014-09-14 23:00:27
Django
paevlk2007, 2014-09-14 23:00:27

How to search on multiple ForeignKey fields?

How to organize a search in one query across fields (accessories_material1, accessories_material2, accessories_material3, accessories_material4, accessories_material5 ).
The model has 5 fields with reference to one model, so that one of the selected fields can reduce the quantity in the Accessories_material model, in which there is an income, and a field with a balance (income - expense = balance).
How can you organize the code, Accessories is one, it has up to 5 Accessories_material fields, you can do ManyToMany, but again you can’t substitute the expense field for each of 1 to 5 selected fields, and reduce the number in Accessories_material. Now, if in ManyToMany it was possible to display two fields of the Accessories_material model, the problem was solved. Who knows tell me!!!

class Accessories(models.Model):
    created_on = models.DateTimeField(u"Дата создания", auto_now_add=True)
    accessories_plot = models.ForeignKey(Accessories_plot, verbose_name='Участок')
    accessories_material1 =  models.ForeignKey(Accessories_material)
    accessories_materialr1 = models.IntegerField(u"Расход Кол-во1")
    accessories_material2 =  models.ForeignKey(Accessories_material)
    accessories_materialr2 = models.IntegerField(u"Расход Кол-во2")
    accessories_material3 =  models.ForeignKey(Accessories_material)
    accessories_materialr3 = models.IntegerField(u"Расход Кол-во3")
    accessories_material4 =  models.ForeignKey(Accessories_material)
    accessories_materialr4 = models.IntegerField(u"Расход Кол-во4")
    accessories_material5 =  models.ForeignKey(Accessories_materia)
    accessories_materialr5 = models.IntegerField(u"Расход Кол-во5")

Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
kzlv, 2014-09-18
@kzlv

It looks like shitty code, to be honest.
Can you describe in more detail? And it's completely unclear what you need. If you answer the question from the title, then you yourself know the answer:

Accessories.objects.filter(accessories_material1__title='раз',
                           accessories_material2__title='два',
                           accessories_material3__title='и так хоть сколько полей')

What you describe is completely incomprehensible.
And you still don’t have the model name in the fifth FK:
accessories_material5 =  models.ForeignKey(Accessories_materia)

P
paevlk2007, 2014-09-18
@paevlk2007

Shitty code due to the fact that:
The "Accessories" model must have at least 5 materials ("Accessories_material" model), the material is selected in the "Accessories" model (the "accessories_material1" field) and the material consumption field is filled in (the "accessories_materialr1" field) . When saving the shape of the "Accessories" model, the material is written off (reduced):

if f.is_valid():
            if accessories_id:
                f.save()
            else:
                accessories = f.save(commit=False)
                accessories.posted_by = request.user
                if accessories.accessories_material:
                    accessories.accessories_material.accessories_material_col_all = ((accessories.accessories_material.accessories_material_col_all) - (accessories.accessories_material_col_ex))
                    accessories.accessories_material.save()
                    accessories.save()
            ...... и тд

model "Accessories_materia":(the name and receipt of the material is entered into the model, the "Accessories"field of the remaining material is filled in through the form "accessories_material_col_all"(income (Accessories_materia -> accessories_material_col)minus consumption (Accessories -> accessories_materialr1)= the remainder of the material
(Accessories_materia -> accessories_material_col_all.)

class Accessories_material(models.Model):
    accessories_material_date = models.DateField(u"Дата прихода")
    accessories_material = models.CharField("Материал:", max_length=100)
    accessories_material_col = models.IntegerField(u"Приход Кол-во", default=0)
    accessories_material_col_all = models.IntegerField(u"Остатки Кол-во", default=0)

class Accessories(models.Model):
    created_on = models.DateTimeField(u"Дата создания", auto_now_add=True)
    accessories_plot = models.ForeignKey(Accessories_plot, verbose_name='Участок')
    accessories_material1 =  models.ForeignKey(Accessories_material)
    accessories_materialr1 = models.IntegerField(u"Расход Кол-во1")
    accessories_material2 =  models.ForeignKey(Accessories_material)
    accessories_materialr2 = models.IntegerField(u"Расход Кол-во2")
    accessories_material3 =  models.ForeignKey(Accessories_material)
    accessories_materialr3 = models.IntegerField(u"Расход Кол-во3")
    accessories_material4 =  models.ForeignKey(Accessories_material)
    accessories_materialr4 = models.IntegerField(u"Расход Кол-во4")
    accessories_material5 =  models.ForeignKey(Accessories_materia)
    accessories_materialr5 = models.IntegerField(u"Расход Кол-во5")

I understand that SHIT CODE ... but for this I wrote here to ask for advice on how to improve the code. How to write off the amount of material using ManyToMany ???

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question