Assign the same value to object keys from lambda in Python (Django) - python-3.x

When trying to assign the same value to starting_bid and price from the Listing model in my Django project the below approach always yields different values. How can I assign the same value while still having the lambda return a random integer whenever the test runs?
tests.py
SEEDER = Seed.seeder() # from django_seed
AMOUNT = 15
MIN = 5
MAX = 84
PRICE = lambda x: random.randint(MIN, MAX)
SEEDER.add_entity(
Listing,
AMOUNT,
{
"starting_bid": PRICE,
"price": PRICE
})
SEEDER.execute()
Result:
{"starting_bid": 80, "price": 45}
Expected:
{"starting_bid": 80, "price": 80}
models.py
class Listing(models.Model):
CATEGORIES = (("LAP", "Laptop"), ("CON", "Console"), ("GAD", "Gadget"), ("GAM", "Game"), ("TEL", "TV"))
user = models.ForeignKey(User, null=True, on_delete=models.CASCADE)
title = models.CharField(max_length=64)
description = models.CharField(max_length=256)
starting_bid = models.PositiveSmallIntegerField()
price = models.PositiveSmallIntegerField()
image_url = models.URLField(max_length=200)
category = models.CharField(max_length=8, choices=CATEGORIES)
active = models.BooleanField(default=True)

_price = []
def PRICE(round = 2):
global _price
if not _price:
_price = random.randint(MIN, MAX)
_price = [_price] * round
return _price.pop()

Related

How can I query item wise stocks and revenue in django ORM?

class RevenueStockDashboardViewset(ViewSet):
sale = InvSaleDetail.objects.filter(sale_main__sale_type='SALE')
sale_returns = InvSaleDetail.objects.filter(sale_main__sale_type='RETURN')
purchase = InvPurchaseDetail.objects.filter(purchase_main__purchase_type='PURCHASE')
purchase_returns = InvPurchaseDetail.objects.filter(purchase_main__purchase_type='RETURN')
def list(self, request):
total_revenue = (self.sale.annotate(total=Sum('sale_main__grand_total'))
.aggregate(total_revenue=Sum('total'))
.get('total_revenue') or 0) - (self.sale_returns.annotate(total=Sum('sale_main__grand_total'))
.aggregate(total_revenue=Sum('total'))
.get('total_revenue') or 0) - (self.purchase.annotate(total=Sum('purchase_main__grand_total'))
.aggregate(total_revenue=Sum('total'))
.get('total_revenue') or 0) + (self.purchase_returns.annotate(total=Sum('purchase_main__grand_total'))
.aggregate(total_revenue=Sum('total'))
.get('total_revenue') or 0)
total_stocks = (self.purchase.annotate(total=Sum('qty'))
.aggregate(total_stock=Sum('total'))
.get('total_stock') or 0) - (self.purchase_returns.annotate(total=Sum('qty'))
.aggregate(total_stocks=Sum('total'))
.get('total_stock') or 0) - (self.sale.annotate(total=Sum('qty'))
.aggregate(total_stock=Sum('total'))
.get('total_stock') or 0) - (self.sale_returns.annotate(total=Sum('qty'))
.aggregate(total_stock=Sum('total'))
.get('total_stock') or 0)
return Response(
{
'total_revenue': total_revenue,
'total_stocks': total_stocks,
# 'item_wise_stocks': items,
}
)
Item Model:
class InvItem(CreatedInfoModel):
name = models.CharField(
max_length=100,
unique=True,
help_text="Item name should be max. of 100 characters",
)
code = models.CharField(
max_length=10,
unique=True,
blank=True,
help_text="Item code should be max. of 10 characters",
)
sale models:
class InvSaleDetail(CreatedInfoModel):
sale_main = models.ForeignKey(
InvSaleMain, related_name="inv_sale_details", on_delete=models.PROTECT
)
item = models.ForeignKey(InvItem, on_delete=models.PROTECT)
item_category = models.ForeignKey(InvItemCategory, on_delete=models.PROTECT)
cost = models.DecimalField(
max_digits=12,
decimal_places=2,
help_text="cost can have max value upto=9999999999.99 and default=0.0",
)
qty = models.DecimalField(
max_digits=12,
decimal_places=2,
help_text="Purchase quantity can have max value upto=9999999999.99 and min_value=0.0",
)
sale_qty = models.DecimalField(
max_digits=12,
decimal_places=2,
help_text="Sale quantity can be max value upto 9999999999.99",
)
purchase Models:
class InvPurchaseDetail(CreatedInfoModel):
purchase_main = models.ForeignKey(
InvPurchaseMain,
related_name="purchase_details",
on_delete=models.PROTECT,
)
item = models.ForeignKey(InvItem, on_delete=models.PROTECT)
item_category = models.ForeignKey(InvItemCategory, on_delete=models.PROTECT)
purchase_cost = models.DecimalField(
max_digits=12,
decimal_places=2,
default=0.0,
help_text="purchase_cost can be max value upto 9999999999.99 and default=0.0",
)
sale_cost = models.DecimalField(
max_digits=12,
decimal_places=2,
help_text="sale_cost can be max value upto 9999999999.99 and default=0.0",
)
qty = models.DecimalField(
max_digits=12,
decimal_places=2,
help_text="Purchase quantity can be max value upto 9999999999.99",
)
I have Inventory Dashboard where I want to show the item wise revenue and item wise stocks. I have done query for total stocks and revenue but I want item wise. If you need more information. Please let me Know.
There is purchase model and sale model, the sale_main and purchase_main is basically sale and puchase info and detail is detail about items we are adding.
I want basically to fetch item wise, calculate revenue as i did and show item wise.

how to work with foreign key field in django

Hi Everyone i am working work django framework, where i used to upload excel file in Dailytrip table, current i get car_mumber from car table, but now i need to store car_number from Car_team table also team_id, i am storing car_id and team_id in car_team table also i need to store team_id in dailytrip table automaticlly based on car_id(car_number) i am to much confuse how to i work that, pls help me out
models.py
class Car_team(BaseModel):
team = models.ForeignKey(
Team,
models.CASCADE,
verbose_name='Team',
null=True,
)
car=models.ForeignKey(
Car,
models.CASCADE,
verbose_name='Car',
null=True)
city =models.ForeignKey(
City,
models.CASCADE,
verbose_name='City',
)
start_date=models.DateField(null=True, blank=True)
end_date=models.DateField(null=True, blank=True)
views.py
def add_payout_uber_daily_data(request):
if request.method == 'POST':
form = UberPerformanceDataForm(request.POST, request.FILES, request=request)
if form.is_valid():
date = form.cleaned_data['date']
excel_file = request.FILES['file']
df = pd.read_excel(excel_file)
is_na = pd.isna(df['Date']).sum().sum() + pd.isna(df['Name']).sum().sum() + pd.isna(df['UUID']).sum().sum() + pd.isna(df['Net Fare With Toll']).sum().sum() + pd.isna(df['Trips']).sum().sum() + pd.isna(df['Uber KMs']).sum().sum() + pd.isna(df['CashCollected']).sum().sum() + pd.isna(df['UberToll']).sum().sum() + pd.isna(df['Tips']).sum().sum() + pd.isna(df['Hours Online']).sum().sum() + pd.isna(df['Ratings']).sum().sum() + pd.isna(df['Acceptance Rate']).sum().sum() + pd.isna(df['Cancellation Rate']).sum().sum()
error_list = []
if is_na > 0:
error_list.append('Found #N/A or blank values in the sheet. Please correct and re-upload')
context = {'error_list': error_list, 'menu_payout': 'active','submenu_daily_data': 'active','form': form, }
return render(request, 'add_payout_uber_daily_data.html', context=context)
date_match = True
for d in df['Date']:
if str(d.strftime("%Y-%m-%d")) != str(date):
date_match = False
break
if not date_match:
error_list.append('Some dates are not matching in excel')
if len(error_list) > 0:
context = {'error_list': error_list, 'menu_payout': 'active','submenu_daily_data': 'active','form': form, }
return render(request, 'add_payout_uber_daily_data.html', context=context)
DailyTrip.objects.filter(date=date).update(is_active=0)
for i in df.index:
uuid = df['UUID'][i]
driver_id = None
car_id = None
fleet_id = None
manager_id = None
try:
driver = Driver.objects.get(uber_uuid=uuid)
driver_id = driver.id
except Driver.DoesNotExist:
driver_id = None
#replce car code and store car_number,car_id,team_id via car_team only this logic need to change current get car_number direct car table but we need car_number vai foriegn key
try:
car = Car.objects.get(car_number=df["Car Number"][i])
car_id = car.id
manager_id = car.manager_id
except Car.DoesNotExist:
car_id = None
try:
fleet = Fleet.objects.get(name=df["Fleet Name"][i])
fleet_id = fleet.id
except Fleet.DoesNotExist:
fleet_id = None
name = df['Name'][i]
car_number = df['Car Number'][i]
fare_total = df['Net Fare With Toll'][i]
trips = df['Trips'][i]
pool_trips = 0
hours_online = df['Hours Online'][i]
total_km = df['Uber KMs'][i]
cash_collected = abs(df['CashCollected'][i])
toll = df['UberToll'][i]
tip_amount = df['Tips'][i]
fare_avg = float(fare_total)/int(trips)
fare_per_hour_online = float(fare_total)/float(hours_online)
fare_per_km = fare_total/total_km
trips_per_hour = trips/hours_online
km_per_trip = total_km/trips
rating = df['Ratings'][i]
acceptance_rate_perc = float(df['Acceptance Rate'][i])/100
driver_cancellation_rate = float(df['Cancellation Rate'][i])/100
obj, created = DailyTrip.all_objects.update_or_create(
date=date, uuid=uuid,
defaults={
'car_id': car_id,
'manager_id': manager_id,
'car_number': car_number,
'driver_id': driver_id,
'car_id': car_id,
'fleet_id': fleet_id,
'driver_name': name,
'fare_total': fare_total,
'trips': trips,
'pool_trips': pool_trips,
'hours_online': hours_online,
'total_km': total_km,
'cash_collected': cash_collected,
'toll': toll,
'tip_amount': tip_amount,
'fare_avg': fare_avg,
'fare_per_hour_online':fare_per_hour_online,
'fare_per_km':fare_per_km,
'trips_per_hour': trips_per_hour,
'km_per_trip': km_per_trip,
'rating': rating,
'acceptance_rate_perc': acceptance_rate_perc,
'driver_cancellation_rate': driver_cancellation_rate,
'is_active': 1,
'comments': None}
)
if len(error_list) > 0:
DailyTrip.objects.filter(date=date).update(is_active=0)
context = {'error_list': error_list, 'menu_payout': 'active','submenu_daily_data': 'active','form': form, }
return render(request, 'add_payout_uber_daily_data.html', context=context)
else:
messages.success(request, 'Daily Trips added Successfully...')
return redirect('/fleet/payout/daily_data/add/uber')
else:
form = UberPerformanceDataForm(initial={})
context = {
'menu_payout': 'active',
'submenu_daily_data': 'active',
'form': form,
}
return render(request, 'add_payout_uber_daily_data.html', context=context)
You can try that :
to get car_number from car_team -->
car_team = car_team.objects.objects.all().last() # to get the last car_team for example
car_number = car_team.car.car_number # to get the car number from the car_team
try:
car = Car.objects.get(car_number=df["Car Number"][i])
car_id = car.id
car1=Car_team.objects.filter(car_id=car_id)
if car1:
team_id=car1[0].team_id
else:
team_id=None
except Car.DoesNotExist:
car_id = None
team_id= None

Django - 'Merging' or Sorting similar objects from a queryset

Good day, I have a django model that represents users (node) that have a relation (edge) with other users. User A is a friend of User B and User A interacted with User B .
class Node(BaseInfo):
firstname = models.CharField(max_length=200,blank = True, null=True)
lastname = models.CharField(max_length=200,blank = True,null=True)
dob = models.DateField(blank=True,null=True)
username = models.CharField(max_length=200, blank=True, null=True)
picture_url = models.CharField(max_length=200,blank = True,null =True)
phone_number = models.CharField(max_length=200,blank = True, null=True)
email = models.CharField(max_length=200,blank = True, null=True)
gender= models.CharField(max_length=200,blank = True, null=True)
uid = models.CharField(max_length=200,blank=True,null=True)
url = models.CharField(max_length=2000,blank=True,null=True)
edges = models.ManyToManyField('self', through='Edge',blank = True)
task = models.ManyToManyField('Task',blank = True)
location = models.ManyToManyField('Location',blank = True)
case = models.ManyToManyField('case',blank = True)
class Edge(BaseInfo):
source = models.ForeignKey('Node', on_delete=models.CASCADE,related_name="is_source")
target = models.ForeignKey('Node', on_delete=models.CASCADE,related_name="is_target")
class Meta:
constraints = [
models.UniqueConstraint(fields=['source','target','label'], name="unique_edge"),
]
def __str__(self):
return '%s' % (self.label)
At this point i query the relations with:
result = Edge.objects.filter(source = User A);
The result will contain a query set with
[Edge(User A , User B , "Friends"),[Edge(User A, User B , "Interacted")]
I want to show the relations (Grouped By ? ) of User A in a template
So it says , Profile A is a friend and has interacted with B. Rather then having to iterate over the queryset and having 2 lines in my template :User A is friends with B , User A interacties with user B.
I already looked in aggregation functions of Django but cant get any further.
Thanks in advance for youre guidance.

Save method of Django model does not update fields of existing record even if force update

I am trying to update the record that already exists in the database and therefore I use this code
if 'supplierId' not in req.keys():
return JsonResponse({'code': 0, 'msg': "supplier was not selected", 'result': ''}, safe=False)
assigneeId = User.objects.get(pk=req.get('assigneeId', 1))
responsibleId = User.objects.get(pk=req.get('responsibleId', 1))
redistributionMethod = req.get('redistributionMethod', 0)
amount = req.get('allCost', 0)
procurement_doc = ProcurementDocJournal.objects.get(id=pk)
print(procurement_doc)
procurement_doc.docType = req['docType']
procurement_doc.status = req['status']
procurement_doc.companyId = Company.objects.get(pk=req['companyId'])
procurement_doc.datetime = req['datetime']
procurement_doc.supplierId = Partner.objects.get(pk=req['supplierId'])
procurement_doc.assigneeId = assigneeId
procurement_doc.warehouseId = Warehouse.objects.get(pk=req['warehouseId'])
procurement_doc.responsibleId = responsibleId
procurement_doc.redistributionMethod = redistributionMethod
procurement_doc.amount = amount
procurement_doc.comment = req['comment']
procurement_doc.save(force_update=True, update_fields=['comment', 'amount', 'redistributionMethod',
'responsibleId', 'warehouseId',
'supplierId', 'datetime',
'companyId', 'assigneeId', 'status', 'docType'])
where req contains a request
something like this
{
'docType': 3,
'status': 1,
'companyId': '2',
'warehouseId': '3',
'assigneeId': '5',
'supplierId': '12671',
'responsibleId': '5',
'datetime': '2020-04-01 08:01:00',
'comment': ''
}
As you can see there is a print which assures me that I selected the correct row
when I noticed that these records are not updated I searched for causes and found
this question where the guy who asked says The message field was missing from the model definition
in my case, none of these are missing from the model's description
class ProcurementDocJournal(models.Model):
id = models.IntegerField(primary_key=True, null=False)
docNumber = models.IntegerField()
status = models.IntegerField()
docType = models.IntegerField()
depended = models.IntegerField()
companyId = models.ForeignKey(Company, on_delete=models.CASCADE,
db_column='companyId')
created_at = models.DateTimeField(auto_now_add=True)
datetime = models.DateTimeField()
currencyId = models.ForeignKey(Currency, db_column='currencyId', on_delete=models.CASCADE)
currencyRate = models.FloatField()
redistributionMethod = models.IntegerField()
assigneeId = models.ForeignKey(User, on_delete=models.CASCADE, related_name='ProcurementDocJournal',
db_column='assigneeId')
warehouseId = models.ForeignKey(Warehouse, on_delete=models.CASCADE,
db_column='warehouseId')
responsibleId = models.ForeignKey(User, on_delete=models.CASCADE, related_name='ProcurementDoc',
db_column='responsibleId')
supplierId = models.ForeignKey(Partner, on_delete=models.CASCADE,
db_column='supplierId')
amount = models.FloatField()
comment = models.TextField()
class Meta:
db_table = 'procurementDocJournal'
get_latest_by = 'id'
Edit
I have an action that contains
procurement_doc_journal_item = ProcurementDocJournal.objects.get(id=pk)
currencyId = req['currency']
currency = Currency.objects.get(id=currencyId)
currencyRate = CurrencyRate(date, currency.name)
procurement_doc_journal_item.currencyId = currency
procurement_doc_journal_item.currencyRate = currencyRate['rate']
procurement_doc_journal_item.save()
and works like a charm
there is no any error that logs say
You don't specify if there's any error in your logs. I'd kind of expect to see something since if it's not saving, it must be bombing out before that as no fields are mandatory.
I am, however, not sure you're setting your FKs correctly here:
procurement_doc.companyId = Company(req['companyId'])
Should be
procurement_doc.companyId = Company.objects.get(pk=req['companyId'])
and that assumes companyId is set to a value that exists of course ... Whether this is the source of your issue or not, I'm not sure. I don't see any of your FKs are mandatory.
Edit: I've just tested this on one of my models with a simple id key:
>>> Contact(1)
Contact()
>>> c = Contact(1)
>>> c.name
''
>>> c = Contact.objects.get(pk=1)
>>> c.name
'Mike'
It really doesn't seem to me as though it works ...
Sidenote: You can simplify code like this:
if 'allCost' not in req.keys():
amount = 0
else:
amount = req['allCost']
By doing:
amount = req.get('allCost', 0)

SQLAlchemy Order joined table by field in another joined table

My project requires that Orders are split into their individual lines which can be displayed in their own views I want these views to order the lines by eta which is a value in the Order table.
I have 3 tables with a 1>1 join on tables 1&2 and a many>many join on tables 2 and 3 defined by table 4 as follows:
class Order(db.Model):
id = db.Column(db.Integer, primary_key=True)
eta = db.Column(db.DateTime())
order_lines = db.relationship('Line', backref='order', order_by=lambda: Line.id)
def __repr__(self):
return '<Order No. {}>'.format(self.increment_id)
class Line(db.Model):
id = db.Column(db.Integer, primary_key=True)
line_name = db.Column(db.String())
order_id = db.Column(db.Integer, db.ForeignKey('order.id'))
product_id = db.Column(db.String, db.ForeignKey('product.product_id'))
def __repr__(self):
return '<Line SKU: {}>'.format(self.line_sku)
class Line_view(db.Model):
id = db.Column(db.Integer, primary_key=True)
view_name = db.Column(db.String())
view_lines = relationship('Line',
secondary='line_view_join',
backref='views',
lazy='dynamic',
order_by= ***???*** ) #ordery by eta on Order table
def __repr__(self):
return '<View: {}>'.format(self.view_name)
class Line_view_join(db.Model):
__tablename__ = 'line_view_join'
id = db.Column(db.Integer(), primary_key=True)
line_id = db.Column(db.Integer(), db.ForeignKey('line.id', ondelete='CASCADE'))
view_id = db.Column(db.Integer(), db.ForeignKey('line_view.id', ondelete='CASCADE'))
I am trying to work out how to query table 3, Line_View and have the joined Lines ordered by the eta of Order table.
Such that when querying:
chosen_view = Line_view.query.filter_by(id = 1).one()
chosen_view.view_lines are ordered by Order.eta
I have Tried
class Line_view(db.Model):
id = db.Column(db.Integer, primary_key=True)
view_name = db.Column(db.String())
view_lines = relationship('Line',
secondary='line_view_join',
backref='views',
lazy='dynamic',
**order_by=lambda: asc(Line.order.eta))**
def __repr__(self):
return '<View: {}>'.format(self.view_name)
But this results in the error:
AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with Line.order has an attribute 'eta'
Do you need to store the Line_views in the database? If not, you can query the Lines sorted by the eta attribute of the related order. Below, I create two orders with one line each, and then query the lines sorted by the eta attribute of their order:
eta = datetime(2019,10,10)
o = Order(eta = eta)
l = Line(order=o, line_name="sample")
db.session.add(o)
db.session.add(l)
eta = datetime(2019,11,11)
o1 = Order(eta = eta)
l1 = Line(order=o1, line_name="sample1")
db.session.add(o1)
db.session.add(l1)
db.session.commit()
lines = Line.query.join(Order).order_by(Order.eta)

Resources