I am trying to adapt the Viewflow Dynamicsplit example
The objective is to split approvals of an Order based on OrderLines and to assign them to the appropriate approver. It seems that this should be possible as there seems to be a possibility described in this answer - Assign user to tasks automatically (Viewflow, Django)
The issue is that whenever I try to grab the current order pk in the DynamicSplitActivation - the queryset comes back empty. Not sure where I am going wrong. The queryset works fine if I set the pk manually, but as soon as I try to use a dynamic variable it stops working.
I have listed the flow and models etc. at the end of the post, however, I am guessing the issue is quite basic. Any help would be appreciated!
nodes.py
...
class DynamicSplitActivation(AbstractGateActivation):
def calculate_next(self):
self._order = self.flow_task._order_callback(self.process)
order_id = self._order.pk
# order_id = 28
order_lines = models.OrderLine.objects.filter(order_id=order_id)
project_lines = project_models.ProjectLine.objects.filter(orderline__in=order_lines)
users = User.objects.filter(projectline__in=project_lines).distinct()
self._approvers = users
...
class DynamicSplit(...):
...
def __init__(self, callback):
super(DynamicSplit, self).__init__()
self._order_callback = callback
self._ifnone_next_node = None
...
flows.py
...
approval_split = (
nodes.DynamicSplit(lambda act: act.order)
.IfNone(this.end)
.Next(this.split_approval_budget)
)
models.py
...
class ProjectLine(models.Model):
project = models.ForeignKey(Project, on_delete=models.CASCADE)
description = models.CharField(max_length=50)
budget_holder = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, blank=True)
class Order(models.Model):
description = models.CharField(max_length=30)
class OrderLine(models.Model):
order = models.ForeignKey(Order, on_delete=models.CASCADE)
project_line = models.ForeignKey(project_models.ProjectLine, on_delete=models.SET_NULL, null=True, blank=False)
product = models.ForeignKey(catalogue_models.Product, on_delete=models.CASCADE)
quantity = models.PositiveIntegerField()
budgetholder_approved = models.BooleanField(null=True)
class OrderProccess(Process):
order = models.ForeignKey(Order, blank=True, null=True, on_delete=models.CASCADE)
...
Related
I have two different models. HitCount model stores IP addresses whose was viewed Post. And what i want is filtering popular 3 posts which viewed more. I've tried some queries but i couldn't. I am sharing my models with you.
class Post(ModelMeta, models.Model):
title = models.CharField(max_length=255, verbose_name='Başlık', unique=True)
slug = models.SlugField(max_length=255, unique=True)
author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='blog_posts', verbose_name="Yazarı")
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='blog_posts',
verbose_name="Kategorisi", null=True)
tags = models.ManyToManyField(Tag, related_name='blog_posts', verbose_name='Etiketler')
image = models.ImageField(verbose_name='Fotoğraf (800x460)')
content = RichTextField()
description = models.TextField(null=True)
status = models.IntegerField(choices=STATUS, default=0, verbose_name='Yayın Durumu')
created_at = models.DateTimeField(auto_now_add=True, verbose_name='Oluşturulma Tarihi')
updated_at = models.DateTimeField(auto_now=True, verbose_name='Güncellenme Tarihi')
#property
def get_hit_count(self):
return HitCount.objects.filter(post=self).count()
class HitCount(models.Model):
ip_address = models.GenericIPAddressField()
post = models.ForeignKey("Post", on_delete=models.CASCADE)
def __str__(self):
return f'{self.ip_address} => {self.post.title}'
You can try something like this :
most_viewed_posts = Post.objects.all().order_by('-get_hit_count')[3]
I don't think that you can order by 'get_hit_count', but I think those questions can help you : Django order_by a property
Using a Django custom model method property in order_by()
I did what i want with sorted method. Thanks Alexandre Boucard for the resources.
Solution;
sorted(Post.objects.filter(status=1), key=lambda a: a.get_hit_count, reverse=True)
reverse=False as a default and it sorts ascending in this case i want to get reversed so i used reverse=True
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 am currently struggling to assign a many-to-many field to my newly created objects. Anyone knows what I am doing wrong?
Note: I have one solution in mind which is first creating the ticket, and then afterwards trying to assign it. Might that be the way to do it?
#Create ticket_tax
assign_event = Event.objects.all()
for event in assign_event:
TicketTax.objects.create(
event=event,
name=lorem,
percentage=0.19,
)
# Create tickets
price_gross = ['40.60', '30.30', '100.40', ]
name = ['Early Bird', 'Regular Ticket', 'Last Minute Ticket', ]
assign_event = Event.objects.all().first()
assign_ticket_tax = TicketTax.objects.all().first()
for i in range(len(price_gross)):
Ticket.objects.create(
event=assign_event,
ticket_tax=assign_ticket_tax.add(),
price_gross=price_gross[i],
name=name[i],
description='ABC',
start_at='2018-05-26 18:12:58.556925+02',
end_at='2018-05-29 18:12:58.556925+02',
quantity=100,
status='On sale',
)
models.py
class TicketTax(models.Model):
event = models.ForeignKey(
Event,
on_delete=models.PROTECT,
related_name='ticket_taxes'
)
name = models.CharField(max_length=100)
percentage = models.DecimalField(
max_digits=5,
decimal_places=4
)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Ticket(models.Model):
event = models.ForeignKey(
Event,
on_delete=models.PROTECT,
related_name='tickets'
)
ticket_tax = models.ManyToManyField(TicketTax, blank=True)
price_gross = models.DecimalField(
max_digits=25,
decimal_places=2
)
name = models.CharField(max_length=100)
description = models.TextField(blank=True, null=True)
start_at = models.DateTimeField()
end_at = models.DateTimeField()
quantity = models.PositiveIntegerField()
status = models.CharField(
max_length=8,
choices=TicketStatus.CHOICES
)
is_archived = models.BooleanField(default=False)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
Note: I have one solution in mind which is first creating the ticket, and then afterwards trying to assign it. Might that be the way to do it?
That is exactly correct. You can't instantiate an object and assign a M2M in one create statement--since both objects need to exist in the database. This inside the for loop should (roughly) do what your code above is trying to do:
ticket = Ticket.objects.create(
event=assign_event,
price_gross=price_gross[i],
name=name[i],
description='ABC',
start_at='2018-05-26 18:12:58.556925+02',
end_at='2018-05-29 18:12:58.556925+02',
quantity=100,
status='On sale',
)
ticket.ticket_tax.add(TicketTax.objects.all().first())
I have these Models all of which have PointField:
class Place(models.Model):
title = models.CharField(max_length=75, verbose_name='Заголовок')
category = models.ForeignKey(PlaceCategory, verbose_name='Категория')
...
point = geomodels.PointField(geography=True, blank=True, null=True)
...
class Event(models.Model):
title = models.CharField(max_length=75, verbose_name='Заголовок')
address = models.CharField(max_length=255, blank=True, null=True, verbose_name='Адрес')
city = models.ForeignKey(City, verbose_name='Город')
...
point = geomodels.PointField(blank=True, null=True)
...
class Meeting(models.Model):
title = models.CharField(max_length=75)
participants = models.ManyToManyField(User, related_name='participating_meetings')
...
point = geomodels.PointField(blank=True, null=True)
...
In project I have /points API url. By this url I want to return filtered set or all the points to show them on map.
I checked DRF documentation, there is example how to build Serializer for one Model, but how do I build the API view for these multiple models?
This kind of use case is typical of where you'd need to drop the default auto generated things (serializer / view) and roll your own. I would gather the data by myself, run them through a PointSerializer - might be optional - which would inherit from Serializer and return the result.
Well, I'm starting learning Django and now I have problems to get a QuerySet that allows me fill a table.
The table shows information about the projects of a programer. Each project has n iterations.
|id|Name|Start|End|Cost|Objetive|Description|Client|Progress|Status| Iterations|
In the View, I can get the first 9 columns (From the Project model) using a QuerySet like this:
projects = Project.objects.filter(responsible = request.user.id)
In the 10th column I have to show the number of "Iterations" of every Project and I'm trying this:
Proyect.objects.filter(responsible = request.user.id).annotate(Niterations= Iteration.objectsfilter(project_id= request.user.id).count())
And well,it doesn't work, I understand that with an annotation the arguments will be added to each object in the QuerySet ...
The other way Iwas trying is:
projects = Project.objects.filter(responsible = request.user.id)
for proy in projects:
proy.annotate(Iteration.objects.filter(project_id=proy.pk).count())
I don't know exactly whay it doesn't works... Is there an easier way?
It's been a little confusing ...
I'm working with two models: "Project" and "Iteration"
Model: Project
from django.db import models
from usuarios.models import User
class Proyect(models.Model):
name = models.CharField(max_length=255)
start = models.DateField()
end = models.DateField(blank=True, null=True)
cost = models.FloatField(blank=True, null=True)
objetive = models.TextField(blank=True, null=True)
description = models.CharField(max_length=255, blank=True, null=True)
client = models.CharField(max_length=255, blank=True,null=True)
progress = models.FloatField(verbose_name=u'percentage of progress', default=0.00, editable=False)
responsible = models.ForeignKey(User, editable=False)
STATUS_CHOICES = (
('A','Atrasado'),
('N','Normal'),
('AP','Alta prioridad'),
)
status = models.CharField(max_length=2, choices=STATUS_CHOICES)
def __unicode__(self):
return self.name
Model: Iteration
from django.db import models
from proyectos.models import Project
class Iteration(models.Model):
identifier = models.CharField(max_length=255)
start = models.DateField()
end = models.DateField()
progress = models.FloatField(default=0.0, editable=False)
project = models.ForeignKey(Project)
Thanks in advance!!!
(I translated this, sorry if there are mistakes in translations)
The right way is:
Project.objects.filter(responsible=request.user.id).annotate(niterations=Count('iteration')
You can see the query to understand better:
result = Project.objects.filter(responsible=request.user.id).annotate(niterations=Count('iteration')
print str(result.query)
I hope I've helped.