I've got two models.
class Color(models.Model):
name = models.CharField(max_length=120, null=True, blank=True)
class Car(models.Model):
user = models.ForeignKey(Color, on_delete=models.CASCADE, default=None)
price = models.DecimalField(max_digits=10, decimal_places=2)
How can I get a queryset of Color instances where the Car instances that are related to those Color instances have a price > 1000?
Thanks!
You can use something like:
related_colors = Color.objects.filter(car_set__price__gt=1000)
Assuming you meant color instead of user? You can filter it as follows:
cars = models.Car.objects.filter(price__gte = 1000).values_list('color', flat = True)
Related
I have a model Allotment
class Kit(models.Model):
kit_types = (('FLC', 'FLC'), ('FSC', 'FSC'), ('Crate', 'Crate'), ('PP Box', 'PP Box'))
kit_name = models.CharField(max_length=500, default=0)
kit_type = models.CharField(max_length=50, default=0, choices=kit_types, blank=True, null=True)
class AllotmentFlow(models.Model):
flow = models.ForeignKey(Flow, on_delete=models.CASCADE)
kit = models.ForeignKey(Kit, on_delete=models.CASCADE)
asked_quantity = models.IntegerField(default=0)
alloted_quantity = models.IntegerField(default=0)
class Allotment(models.Model):
transaction_no = models.IntegerField(default=0)
dispatch_date = models.DateTimeField(default=datetime.now)
send_from_warehouse = models.ForeignKey(Warehouse, on_delete=models.CASCADE)
flows = models.ManyToManyField(AllotmentFlow)
For a stacked graph I am trying to get the data of different kit_type alloted in different months.
For that I have tried annotate but it isn't getting the desired results
dataset = Allotment.objects.all().annotate(
month=TruncMonth('dispatch_date')).values(
'month').annotate(dcount=Count('flows__kit__kit_type')).values('month', 'dcount')
Expected Output:
[{'month':xyz, 'kit_type':foo, count:123},...]
I am getting the month and count of kit type from above but how do I segregate it by kit_type?
having a field that represents your choice field names in this query is difficult
instead how about use the Count filter argument and annotate to get what you want
dataset = Allotment.objects.all().annotate(month=TruncMonth('dispatch_date')).values('month').annotate(
FLC_count=Count('flows__kit__kit_type', filter=Q(flows__kit__kit_type="FLC")),
FSC_count=Count('flows__kit__kit_type', filter=Q(flows__kit__kit_type="FSC")),
Crate_count=Count('flows__kit__kit_type', filter=Q(flows__kit__kit_type="Crate")),
PP_Box_count=Count('flows__kit__kit_type', filter=Q(flows__kit__kit_type="PP_Box")),
).values('month', 'FLC_count', 'FSC_count', 'Crate_count', 'PP_Box_count')
I am trying to create an E-Commerce Website and I am at the Final Step i.e. Placing the Order. So, I am trying to add all the Cart Items into my Shipment model. But I am getting this error.
'QuerySet' object has no attribute 'product'
Here are my models
class Product(models.Model):
productId = models.AutoField(primary_key=True)
productName = models.CharField(max_length=200)
productDescription = models.CharField(max_length=500)
productRealPrice = models.IntegerField()
productDiscountedPrice = models.IntegerField()
productImage = models.ImageField()
productInformation = RichTextField()
productTotalQty = models.IntegerField()
alias = models.CharField(max_length=200)
url = models.CharField(max_length=200, blank=True, null=True)
class Customer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
name = models.CharField(max_length=100, null=True, blank=True)
email = models.EmailField(max_length=100)
profileImage = models.ImageField(blank=True, null=True, default='profile.png')
phoneNumber = models.CharField(max_length=10, blank=True, null=True)
address = models.CharField(max_length=500, blank=True, null=True)
class Order(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, blank=True, null=True)
dateOrdered = models.DateTimeField(auto_now_add=True)
orderCompleted = models.BooleanField(default=False)
transactionId = models.AutoField(primary_key=True)
class Cart(models.Model):
product = models.ForeignKey(Product, on_delete=models.SET_NULL, blank=True, null=True)
order = models.ForeignKey(Order, on_delete=models.SET_NULL, blank=True, null=True)
quantity = models.IntegerField(default=0, blank=True, null=True)
dateAdded = models.DateTimeField(auto_now_add=True)
class Shipment(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, blank=True, null=True)
orderId = models.CharField(max_length=100)
products = models.ManyToManyField(Product)
orderDate = models.CharField(max_length=100)
address = models.CharField(max_length=200)
phoneNumber = models.CharField(max_length=13)
I just removed additional functions i.e. __str__ and others.
Here is the views.py
def orderSuccessful(request):
number = Customer.objects.filter(user=request.user).values('phoneNumber')
fullAddress = Customer.objects.filter(user=request.user).values('address')
timeIn = time.time() * 1000 # convert current time in milliSecond
if request.method == 'POST':
order = Shipment.objects.create(customer=request.user.customer, orderId=timeIn,
orderDate=datetime.datetime.now(), address=fullAddress,
phoneNumber=number)
user = Customer.objects.get(user=request.user)
preOrder = Order.objects.filter(customer=user)
orders = Order.objects.get(customer=request.user.customer, orderCompleted=False)
items = orders.cart_set.all() # Here is all the items of cart
for product in items:
product = Product.objects.filter(productId=items.product.productId) # error is on this line
order.products.add(product)
Cart.objects.filter(order=preOrder).delete()
preOrder.delete()
order.save()
else:
return HttpResponse("Problem in Placing the Order")
context = {
'shipment': Shipment.objects.get(customer=request.user.customer)
}
return render(request, "Amazon/order_success.html", context)
How to resolve this error and all the cart items to field products in Shipment model?
Your model is not really consistent at all. Your Cart object is an m:n (or m2m - ManyToMany) relationship between Product and Order. Usually, you would have a 1:n between Cart and Product (a cart contains one or more products). One Cart might be one Order (unless you would allow more than one carts per order). And a shipment is usually a 1:1 for an order. I do not see any of this relationships in your model.
Draw your model down and illustrate the relations between them first - asking yourself, if it should be a 1:1, 1:n or m:n? The latter can be realized with a "through" model which is necessary if you need attributes like quantities.
In this excample, we have one or more customers placing an order filling a cart with several products in different quantities. The order will also need a shipment fee.
By the way: bear in mind that "filter()" returns a list. If you are filtering on user, which is a one to one to a unique User instance, you would better use "get()" as it returns a single instance.
Putting in into a try - except or using get_object_or_404() makes it more stable.
product = Product.objects.filter(productId=items.product.productId)
should be something like:
product = product.product
not to say, it becomes obsolete.
It looks like you make a cart for a product by multiple instances of Cart, the problem is you try to access the wrong variable, also you don't need to filter again when you already have the instance, make the following changes:
carts = orders.cart_set.all() # Renamed items to carts for clarity
for cart in carts:
product = cart.product
order.products.add(product) # The name order is very misleading makes one think it is an instance of Order, actually it is an instance of Shipment
As mentioned above in my comment your variable names are somewhat misleading, please give names that make sense to any variable.
I have two models:
Lot:
class Lot(models.Model):
name = models.CharField(max_length=150, db_index=True, unique=True)
step = models.DecimalField(max_digits=2, decimal_places=2)
and Bid:
class Bid(models.Model):
auction = models.ForeignKey('Lot', on_delete=models.CASCADE)
user_id = models.ForeignKey(User, on_delete=models.CASCADE, to_field='username')
value = models.DecimalField(max_digits=5, decimal_places=2)
Every instance of Lot can have a few Bids, however any instance of Bid is only related to a particular Lot.
I have a working annotation for Lot that gives me the max_bid and next_bid values:
self.auc_set = Lot.objects.annotate(max_bid=Max('bid__value'), next_bid=(Max('bid__value') + F('step')))
And what i can't achieve is getting 3 annotated fields: max_bid, next_bid and last_bidder.
Something like:
self.auc_set = Lot.objects.annotate(max_bid=Max('bid__value'), next_bid=(Max('bid__value') + F('step')), last_bidder=F(bid_set).get('auction_id'= F('id'), 'value'=max_bid)['user_id'])
but working.
Update:
The problem would be solved if i knew how to pass the 'id' from Lot.objects.annotate to the Bid.objects.get part:
auc_set = Lot.objects.annotate(last_bidder=Bid.objects.get(auction_id__exact='need_to_put_something_here', value=Max('value').user_id)
I have a product model with the following:
class Product(models.Model):
name = models.CharField(max_length=255)
handle = models.CharField(max_length=55)
summary = models.CharField(max_length=255, blank=True)
category = models.ForeignKey(Category)
Above is just the product detail, but there are 3 types of pricing:
Standard - normal regular pricing by filling the price field
Variant - pricing with product variants (size, colour, etc.) with their respective prices
Combined - this is a combination of other saved products, with a custom price provided by user.
For 1 and 2 I have the below model. If the product model has more than 1 price on StandardProduct model then I know it has variants.
class StandardProduct(models.Model):
variant_type = models.CharField(max_length=55)
variant_name = models.CharField(max_lenght=55)
product = models.ForeignKey(Product)
sku = models.CharField(max_length=55, blank=True, null=True)
barcode = models.CharField(max_length=55, blank=True, null=True)
selling_price = models.DecimalField(max_length=15, decimal_places=2)
How do I go about creating the CombinedProduct model? The combined product model can have different created products inside (with their quantities). The price is specified by the user. Below is what I have, but I don't know how to approach this.
class CombinedProduct(models.Model):
product = models.ForeignKey(Product)
item = models.ForeignKey(StandardProduct)
quantity = models.DecimalField(max_length=15, decimal_places=2)
How do I go about creating the CombinedProduct model? The combined
product model can have different created products inside (with their
quantities). The price is specified by the user. Below is what I have,
but I don't know how to approach this.
What you probably want is a ManyToManyField with through option.
This is just a very rough sketch up - I don't get your Product/StandardProduct and the relations.
class CombinedProduct(models.Model):
products = models.ManyToManyField(
Product,
through='Combination'
)
class Combination(models.Model):
product = models.ForeignKey(Product)
combined_product = models.ForeignKey(CombinedProduct)
price = models.DecimalField(max_length=15, decimal_places=2)
quantity = models.DecimalField(max_length=15, decimal_places=2)
I have a model which looks like this
player_info, game, score, creation_date
I want to fetch the records with the highest score of each player for a particular game.
Any help is appreciated.
EDIT:
class Game(models.Model):
name = models.CharField(max_length=50)
description = models.TextField(max_length=1000)
logo = models.URLField()
resource_info = models.URLField()
cost = models.DecimalField(default=0.0, decimal_places=2, max_digits=10)
modified_date = models.DateTimeField(auto_now_add=True)
developer_info = models.ForeignKey(User, related_name='uploaded_games', on_delete=models.CASCADE)
class Score(models.Model):
game_info = models.ForeignKey(Game, related_name='game_info',on_delete=models.CASCADE)
player_info = models.ForeignKey(User, related_name='player_info', on_delete=models.CASCADE)
last_played = models.DateTimeField(auto_now=True)
score = models.BigIntegerField(default=0)
If I understand correctly:
You want to filter your Scores by a Game.
And then you want to GROUP BY User and get the MAX score
The GROUP BY part is a bit tricky, but you can achieve it using values + annotate.
Try something like this:
from django.db.models import Max
Score.objects.filter(game_info=...)
.values('player_info').annotate(max=Max('score'))
More info an examples: https://docs.djangoproject.com/en/1.10/topics/db/aggregation/