简体   繁体   中英

how do you assign a foreign key to a boolean field in another table in django

hello i am trying to assign a foreign key to a boolean field in my User table which is called seller

here is my code for my code of which is want to have a foreign key assigned to my seller field, which is a boolean field


class MyMarketplaceManager(BaseUserManager):
    def create_user(self, business_name, foods, seller):
        if not business_name:
            raise ValueError("Users must have a email")
        if not seller:
            raise ValueError("Users must have a password")
        if not foods:
            raise ValueError("Sellers must have a menu")
        user = self.model(
            business_name=business_name,
            seller=seller,
            food_item=foods
        )
        user.save(user=self.db)
        return user




class Marketplace(models.Model):
    business_name = models.CharField(max_length=32, default="", primary_key=True)
    seller_id = models.ForeignKey(User, on_delete=models.CASCADE)
    foods = ArrayField(models.CharField(max_length=200), blank=True)
    objects = MyMarketplaceManager()

the seller id field is what i want to assign the seller field to i have seen on another question which asks about a primary key which was this

How to set foreign key to a field of another model?

but it didn't have anything about other fields

can anyone help?

You already have user as a FK seller_id = models.ForeignKey(User, on_delete=models.CASCADE) If you want to check if that user is a seller. Assuming this boolean field is called is_seller

User.objects.filter(is_seller=True)

This command fill give you all users who are sellers. You can filter it even more to suit your needs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM