Sakib ovi
Sakib ovi

Reputation: 557

How can i update a specific field using foreign key relationship

I want to update a field which is called level. My relation is like this. i am using django User model. Which is extended to Extended user like this below:-

class ExtendedUser(models.Model):
    levels_fields = (
        ("NEW SELLER", "NEW SELLER"),
        ("LEVEL1", "LEVEL1"),
        ("LEVEL 2", "LEVEL 2"),
        ("LEVEL 3", "LEVEL 3")
    )
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    email = models.EmailField(null=True, unique=True)
    contact_no = models.CharField(max_length=15, null=True)
    profile_picture = models.ImageField(upload_to="images/", null=True)
    country = models.ForeignKey(Country, on_delete=models.CASCADE, null=True)
    city = models.ForeignKey(City, on_delete=models.CASCADE, null=True)
    level = models.CharField(max_length = 120, null=True, blank=True, choices=levels_fields, default="NEW SELLER")

I have a field call "Level" which is full of choices. Now i have a model named Checkout

This is the checkout model :-

class Checkout(models.Model):
    ORDER_CHOICES = (
        ("ACTIVE", "ACTIVE"),
        ("LATE", "LATE"),
        ("DELIVERED", "DELIVERED"),
        ("CANCELLED", "CANCELLED"),
    )
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    seller = models.ForeignKey(
        User, on_delete=models.CASCADE, null=True, related_name='seller')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now_add=True)
    first_name = models.CharField(max_length=120)
    last_name = models.CharField(max_length=120)
    package = models.ForeignKey(OfferManager, on_delete=models.CASCADE)
    quantity = models.PositiveIntegerField(default=0)
    price = models.DecimalField(decimal_places=2, max_digits=10, default=0.00)
    total = models.DecimalField(decimal_places=2, max_digits=10, default=0.00)
    grand_total = models.DecimalField(
        decimal_places=2, max_digits=10, default=0.00, null=True)
    paid = models.BooleanField(default=False)
    due_date = models.DateField(null=True)
    order_status = models.CharField(max_length=200, choices=ORDER_CHOICES, default="ACTIVE")
    is_complete = models.BooleanField(default=False, null=True)

I have another model which is called SellerSubmit and the checkout is the foreign key here. Here is the model :-

class SellerSubmit(models.Model):
    checkout = models.ForeignKey(Checkout, on_delete=models.CASCADE, related_name="checkout")
    file_field = models.FileField(upload_to="files/", null=True)
    submit_date = models.DateField(auto_now_add=True, null=True)

    def __str__(self):
        return str(self.id)

    
    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)

Now when a seller post in the SUbmitModel it will update the level to LEVEL1. How can do it,

This is what i tried :-

def sellerSubmitView(request, pk):
    form = SellerSubmitForm()

    if request.method == "POST":
        file_field = request.FILES.get("file_field")
        try:
            checkout = Checkout.objects.get(id=pk)
            # print(file_field)
        except:
            return redirect("manage-order")
        else:
            SellerSubmit.objects.create(checkout=checkout, file_field=file_field)
            checkout.order_status ="DELIVERED"
            
            chk = checkout.is_complete = True
            
            l = Checkout.objects.filter(is_complete = True).filter(seller=request.user).count()
            print("Count:" + str(l))
            
            
            if l > 5:
                out = checkout.seller.extendeduser.level = "LEVEL1"
                print(out)
                checkout.save()
        
            print(chk)
            checkout.save()
            
            
            
            return redirect("manage-order")

    args = {
        "form": form,
        "checkout_id": pk,
    }
    return render(request, "wasekPart/sellerSubmit.html", args)

Upvotes: -1

Views: 124

Answers (2)

Sabil
Sabil

Reputation: 4510

This should solve your problem

out = checkout.seller.extendeduser.level = "LEVEL1"
print(out)
checkout.seller.extendeduser.save()
checkout.save()

It's my bad that didn't notice ExtendedUser is not extended from User.

Please let me know if it's worked or not

Upvotes: 1

Hasibur Rahman Omi
Hasibur Rahman Omi

Reputation: 109

checkout.seller.extendeduser.level = "LEVEL1" checkout.seller.extendeduser.save()

Upvotes: 1

Related Questions