Home > Software engineering >  How to store multiple values inside a foreign key field?
How to store multiple values inside a foreign key field?

Time:06-28

I need to add multiple categories to a brand field. Here is the code

class Brand_Category(models.Model):
    """
        this model is use to store the data of products category
    """
    name = models.CharField(max_length=50, unique=True, verbose_name="Brand Category Name")
    slug = models.SlugField(max_length=140, unique=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True)


class Brand(models.Model):
    """
        This model is use to store the data of products sub category
    """
    user = models.ForeignKey(User, related_name='brand_user', on_delete=models.CASCADE,null=True,blank=True, default=None)
    name = models.CharField(max_length=50, unique=True, verbose_name="Brand Name")
    brand_img = models.FileField(verbose_name='Upload Image')
    category = models.ForeignKey(Brand_Category, related_name='brand_category', on_delete=models.PROTECT, null=True)
    slug = models.SlugField(max_length=140, unique=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True)

I have a brand form where I am adding multiple categories. This categories are stored in the brand_category model, and i would like to save the id of multiple categories into one brand field. how can i add that? i have found about onetomany field in django but it seems to have deprecated, and the other soultions are not similar to my problem. However, a cateogry is not strictly related to any brand.

Here is the insertion code

brand_name = request.POST["brand_name"]
        image = request.FILES['image']
        brand_category = request.POST.getlist("brand_category")
        
        brand = models.Brand.objects.create(
            user = request.user,
            name = brand_name,
            brand_img = image,
            category_id = brand_category,
        ).save()

CodePudding user response:

Try using many to many relationships as the brand_category_field. With this relationship you can have one brand related to multiple categories and yet a category can not be restricted to a single brand.

CodePudding user response:

Foreign Key is One to Many Relation. In your case, one category can have many Many Brands so if the brand has to be in another category also means in that case you have to change the relationship from Foreign key relation to Many-to-Many Relation.

Check the below link for reference

https://docs.djangoproject.com/en/4.0/topics/db/examples/many_to_many/


class Brand(models.Model):
    """
        This model is use to store the data of products sub category
    """
    user = models.ForeignKey(User, related_name='brand_user', on_delete=models.CASCADE,null=True,blank=True, default=None)
    name = models.CharField(max_length=50, unique=True, verbose_name="Brand Name")
    brand_img = models.FileField(verbose_name='Upload Image')
    category = models.ManyToManyField(Brand_Category,related_name='brand_category')
    slug = models.SlugField(max_length=140, unique=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True)


  • Related