Search code examples
pythondjangodjango-orm

Validate Model wasn't added to m2m field in another Model yet


I have 2 models:

class UserInventory(models.Model):
    user = models.OneToOneField(verbose_name='user', to='users.User', on_delete=models.CASCADE)
    
    # items
    promocodes = models.ManyToManyField(verbose_name="promos", to="inventory.PromocodeItem", blank=True)

class PromocodeItem(models.Model):
    name = models.CharField(verbose_name='code name', max_length=128)
    code = models.CharField(verbose_name='code', max_length=512, db_index=True)
    is_used = models.BooleanField(verbose_name='used?', default=False)

My API can add codes to the user_inventory model. I want to create a manual admin APIView to give codes to users. In this view, I also want to check if this code already exists in anyone's inventory.

I have this code:

def post(self, request, code_id, user_id):
    code = PromocodeItem.objects.filter(id=code_id).first()
    user = User.objects.filter(id=user_id).first()
    inv = UserInventory.objects.get_or_create(user=user)

    // Here I need to validate that the code model has not been added to anyone`s user inventory yet.

    inv.promocodes.add(code)
    return Response("ok")

Which is the best way to do this?


Solution

  • First, define the related_name in your ManytoManyField.

    promocodes = models.ManyToManyField(
        verbose_name="promos",
        to="inventory.PromocodeItem",
        blank=True,
        related_name="user_inventories"
    )
    

    Then, you can fetch the UserInventory models from PromocodeItem reversely:

    code = PromocodeItem.objects.filter(id=code_id).first()
    code_users = code.user_inventories.all()
    
    # Or in your case, just to check if user is added.
    is_added = code.user_inventories.exists()