M2M through table to count q-ty of relation Model

Hi everyone.
I’m struggling with implementation of some writing off procedure/architecture. Let me explain in details:

Lets say I have some Device model which has some Config as well:

class Device(models.Model):
    config = models.ForeignKey(Config)    

class Config(models.Model):
    camera = models.ForeignKey(Camera)
    other_part = models.ForeignKey(...)

class Camera(models.Model):
    item = models.OneToOneField(StockItem)

class StockItem(models.Model):
    stock = models.ForeignKey(Stock)
    brand = models.CharField()
    model = models.CharField()
    category = models.ForeignKey(Category)

The main goal is to know how many and which cameras are used when Device created. An later use that quantity to decrement that position from a particular stock.

Problem:
As I have OneToOne relation between StockItem and Camera I can’t add quantity field to Camera model as this q-ty will be constantly fixed for all relations with this camera. Then where I can keep the cameras’ q-ty as it may be 1-3 cameras in one Device and duplacates allowed (2 cameras are exactly same and 1 different).

First guess was to implement M2M relations for Config and manualy create through table:

class Config(models.Model):
    cameras = models.ManyToManyField(Camera, through="CameraSetup")

class CameraSetup(models.Model):
    camera = models.ForeignKey(Camera)
    config = models.ForeignKey(Config)
    quantity = models.IntegerField()

That might work but looks complicated in filtering. Whenever I create a new Device I need to search if Config exists to avoid duplicates. Is there another way for implementation such a functionality? Because I’m not able to find it so far.
Would appreciate any ideas. Thanks in advance.

Your explanation is not clear.
In particular, I don’t understand why you would use a model with only one-to-one fields.

Please clearly explain the role of each model and what outcomes you want to achieve.

I have a stock position model - StockItem which represents records of items remained on stock e.g name, q-ty. Every part used to “build” Device (Camera in this case) must be linked with StockItem. Therefore it seems right OneToOneField for this relation as StockItem object like Camera might be related with single Camera instance. And whenever new device created user may choose from all cameras available on stock without duplicates.

Short explain every model:

  1. StockItem. Base model represents all items are on stock. Even some keyboards, displays etc.
  2. Config. Configuration of every device being assembled. Let say that is PC. It may consist of Motherboard, CPU etc. It has relation with other parts which important for setup and represents hardware of PC.
  3. Device itself. Represents model which has some config. Top level of this abstraction enclosing “child” parts.

So I need to count those “hardware” items related to Config model e.g. Motherboard, CPU and whatever is there inside (excluding some small parts may be ignored). Hope its clarified my goal a little.

Where is the code for that?

It is in the topic’s first post. Just updated with additional fields if that matters but I don’t see how it related my question.

So your question is that your filtering is complicated, and you ask how you can simplify it?!

Please also add an example of the desired result.

Not quite. I’m looking a right solution for a problem described in a head post of this topic. And difficulties in a filtering is a result of my solution for this moment which I also described in a first topic (M2M relations with through model). So I’m looking for better one if it exists.

I don’t have an example of desired result as I don’t know how to implement such a functionality with right DB architecture.
The only desired result I need :
Feature of writing off every Item in Config model from a particular Stock. In my example it’s Camera which has 1to1 relation with StockItem (responsible for keeping records of items presented on Stock). I want to be able to decrement those cameras installed in Device from Stock.