Good day,
I am trying to build a system that creates an order and allows a user to add pieces. Here are the two models:
class Order(models.Model):
order_slug = models.SlugField(unique=True, blank=True)
machine = models.ForeignKey(Machine, blank=True,on_delete=models.DO_NOTHING)
production_type = models.CharField(choices=PRODUCTION_TYPE_CHOICES, max_length=30)
production_bond = models.CharField(choices=PRODUCTION_BOND_CHOICES, max_length=50)
profile = models.CharField(choices=PROFILE_CHOICES, max_length=50)
order_number = models.CharField(max_length=50)
order_colour = models.CharField(choices=ORDER_COLOUR_CHOICES, max_length=50)
order_finish = models.CharField(choices=ORDER_FINISH_CHOICES, max_length=10)
order_gauge = models.IntegerField(choices=ORDER_GAUGE_CHOICES)
order_width = models.IntegerField()
date_received = models.DateTimeField(default=datetime.now)
class Meta:
ordering = ["date_received", "-profile"]
def __str__(self):
return (str(self.order_slug))
class Piece(models.Model):
order = models.ForeignKey(Order,on_delete=models.CASCADE)
coil = models.ForeignKey(Cut_Material ,on_delete=models.CASCADE)
piece_length = models.DecimalField(max_digits=4, decimal_places=2)
prime_pieces = models.IntegerField()
reject_pieces = models.IntegerField()
coil_constant = models.IntegerField(blank=True)
def __str__(self):
return (str(self.piece_length))
class Meta:
ordering = ["-order","piece_length"]
Here is what I want to do.
Multiply the prime_pieces by the piece_length to get running_meters for each column
I want to group the pieces by order_gauge, order_number, and sum the running_meters.
Proposed solution
piece = Piece.objects.all()
order = Order.objects.all()
For step 1
rm_data = Piece.objects.annotate(running_meters=ExpressionWrapper(F('piece_length') * F('prime_pieces'), output_field=FloatField())).values()
For step 2
I am not sure
Can someone help me please?
I have tried this and it seems to work after scratching my head:
order.values('order_number','order_gauge').annotate(
running_meters=Sum(ExpressionWrapper(F('piece__piece_length')*F('piece__prime_pieces'),output_field=FloatField())))
Related
I have a Warehouse model like the following:
class ShelfBin(models.Model):
bin_id = models.IntegerField(default=0)
bin_name = models.CharField(max_length=50, default=0)
class UnitShelf(models.Model):
shelf_id = models.IntegerField(default=0)
shelf_name = models.CharField(max_length=50, default=0)
bin = models.ManyToManyField(ShelfBin, blank=True)
class AisleUnit(models.Model):
unit_id = models.IntegerField(default=0)
unit_name = models.CharField(max_length=50, default=0)
shelf = models.ManyToManyField(UnitShelf, blank=True)
class ZoneAisle(models.Model):
aisle_id = models.IntegerField(default=0)
aisle_name = models.CharField(max_length=50, default=0)
unit = models.ManyToManyField(AisleUnit, blank=True)
class WarehouseZone(models.Model):
zone_id = models.IntegerField(default=0)
zone_name = models.CharField(max_length=50, default=0)
aisle = models.ManyToManyField(ZoneAisle, blank=True)
class Warehouse(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=250, default=0)
address = models.CharField(max_length=500, default=0)
zones = models.ManyToManyField(WarehouseZone, blank=True)
for this I have created a serializer like the following:
class WarehouseSerializer(serializers.ModelSerializer):
zones = WarehouseZonesSerializer(many=True)
class Meta:
model = Warehouse
fields = "__all__"
def create(self, validated_data):
print("validated data warehouse", validated_data)
zone_objects = validated_data.pop('zones', None)
instance = Warehouse.objects.create(**validated_data)
for item in zone_objects:
aisle_objects = item.pop('aisle')
wz_obj = WarehouseZone.objects.create(**item)
for data in aisle_objects:
unit_objects = data.pop('unit')
za_obj = ZoneAisle.objects.create(**data)
for u_data in unit_objects:
shelf_objects = u_data.pop('shelf')
au_obj = AisleUnit.objects.create(**u_data)
for s_data in shelf_objects:
bin_objects = s_data.pop('bin')
us_obj = UnitShelf.objects.create(**s_data)
for b_data in bin_objects:
b_obj = ShelfBin.objects.create(**b_data)
us_obj.bin.add(b_obj)
au_obj.shelf.add(us_obj)
za_obj.unit.add(au_obj)
wz_obj.aisle.add(za_obj)
instance.zones.add(wz_obj)
return instance
Now the problem is that sometimes warehouse can have zone, aisle, units, etc(all 5 sub-levels) but sometimes it can only be 1,2 or 0 level deep
and in that cases it raises error like this :
aisle_objects = item.pop('aisle')
KeyError: 'aisle'
So do I have to use try and except at each level of the loop or is there a better way to handle these exceptions?
I want to create a new object in ModelB when specific condition are met in ModelA. I am new to Django so that I am unable to figure out how exactly I can achieve this.
For example I have two models(Product and ProductVariant), when specific condition on ProductVariant is met then I want to calculate new object value in Product model.
My Product model is like this:
PRODUCT_TYPE = (
('s', 'simple'),
('v', 'varaible')
)
class Products(models.Model):
name = models.CharField(max_length=250,null=True, blank=True,)
slug = models.SlugField(max_length=200, unique=True,null=True)
short_description = HTMLField()
description = HTMLField()
category = models.ForeignKey(Categories, related_name="products",on_delete=models.SET_NULL,null=True,blank=True,)
brand = models.ForeignKey(Brands,on_delete=models.CASCADE, default=None, null=True, blank=True,)
warranty_support = HTMLField()
product_type = models.CharField(choices=PRODUCT_TYPE, default='simple', max_length=50)
And my Product Attribute Model is like this:
class ProductVariant(models.Model):
product = models.ForeignKey(Products,on_delete=models.CASCADE)
variant = models.ForeignKey(ProductAttribute,on_delete=models.CASCADE, null = True, default=None)
managed_stock = models.IntegerField(choices=STOCK_MANAGED, default=0)
stock = models.IntegerField(default=None)
stock_threshold = models.IntegerField()
price = models.DecimalField(max_digits=10, decimal_places=2)
sku = models.CharField(max_length= 250, default=None)
sale_price = models.DecimalField(max_digits=10, decimal_places=2)
sale_start_date=models.DateField(auto_now_add=False, auto_now=False, default=None)
sale_end_date=models.DateField(auto_now_add=False, auto_now=False,default=None)
I am trying to create regular_price and sale_price on Product model if product_type is variable and if sale_end_date is greater than today. I want to set the price from the variant which has the lowest price.
I tried doing like this on Product model:
def clean(self):
if self.product_type == 'varaible' and ProductVariant.objects.filter(product=self, variant_count__gt = 1):
self.min_price = ProductVariant.objects.filter(product=self).Min('price')
self.max_price = ProductVariant.objects.filter(product=self).Max('price')
but I am not able to achieve what I want,
How can I do this?
After some research and analysis I found solution to my problem, I am posting the solution here so that someone with similar problem could be benefited.
#property
def get_price(self):
result = dict()
variants = ProductVariant.objects.filter(product=self)
count = variants.count()
if count > 1:
min_variant = variants.order_by('price').first()
max_variant = variants.order_by('-price').first()
result['min_price'] = min_variant.price
result['max_price'] = max_variant.price
elif count == 1:
variant = variants.first()
if variant.sale_price:
result['price'] = variant.price
result['sale_price'] = variant.sale_price
sale_variant = variants.order_by('sale_price').first()
result['lowest_sale_price'] = sale_variant.sale_price
result['regular_price'] = sale_variant.price
today = datetime.date.today()
if variant.sale_start_date <= today and variant.sale_end_date >= today:
result['sale_end_date'] = variant.sale_end_date
else:
result['price'] = variant.price
My model
class Ad_company(models.Model):
idx = models.AutoField(primary_key=True)
subject = models.CharField(max_length=255)
memo = models.CharField(max_length=255)
content = models.TextField()
is_display = models.CharField(max_length=1)
writer = models.CharField(max_length=255)
write_date = models.DateTimeField()
update_date = models.DateTimeField()
delete_date = models.DateTimeField()
deadline_date = models.DateTimeField()
reply = models.IntegerField(blank=True)
hits = models.IntegerField(blank=True)
ad_apply = models.IntegerField(blank=True)
ad_category1 = models.CharField(max_length=255)
ad_category2 = models.CharField(max_length=255)
ad_place = models.CharField(max_length=255)
ad_age = models.CharField(max_length=255)
ad_sex = models.CharField(max_length=255)
ad_budget = models.BigIntegerField()
ad_length = models.CharField(max_length=255)
is_done = models.CharField(max_length=1)
is_pay = models.CharField(max_length=1)
ad_service = models.CharField(max_length=255)
ad_object = models.CharField(max_length=255)
is_file = models.CharField(max_length=1)
ad_require = models.CharField(max_length=255)
class Meta:
managed = False
db_table = 'ad_write_company'
class Ad_company_apply(models.Model):
idx = models.AutoField(primary_key=True)
parent_idx = models.IntegerField()
username = models.CharField(max_length=255)
content = models.TextField()
date = models.DateTimeField(default=datetime.now, blank=True)
budget = models.BigIntegerField()
term = models.IntegerField()
is_done = models.CharField(max_length=1)
SELECT * FROM ad_write_company INNER JOIN ad_write_company_apply ON ad_write_company.idx = ad_write_company_apply.parent_idx where ad_write_company_apply.is_done = 1 and ad_write_company_apply.username = 'asdffdsa'
This is my query. but I can not make join query with orm.
Sorry for question is too short.
And Is my query right?
I not sure of that. Thanks for answer.
or do you guys have other good idea?
I would advise to work with a ForeignKey from Ad_company_apply to Ad_company. This makes it easier to generate queries in Django and will guarantee referential integrity.
It thus makes sense to rewrite the Ad_company_apply model to:
class Ad_company_apply(models.Model):
# …
parent_idx = models.ForeignKey(
Ad_company,
db_column='parent_idx',
on_delete=models.CASCADE
)
# …
In that case, you can .filter(…) [Django-doc] with:
Ad_Company.objects.filter(ad_company_appy__isdone=1, ad_company_appy__username='asdffdsa')
Note: Models in Django are written in PascalCase, not snake_case,
so you might want to rename the model from Ad_company to AdCompany.
It is necessary that after the addition of the series, the number of
the series is automatically added, if the administrator forgets to
add, in this way: we take the last created series, from there we take
the series number, and add to this the number of the series 1, and add
to our series! But constantly vylazyut such errors as:
1) lacks the
argument "self", add it (although why it is there at all, it is not
known) and still does not work!
this is my models and SIGNALS
class Series(models.Model):
id = models.AutoField(primary_key=True)
rus_name = models.CharField(max_length=60)
eng_name = models.CharField(max_length=60)
slug = models.SlugField(unique=False)
serial_of_this_series = models.ForeignKey(Serial, on_delete=models.CASCADE, default=True)
season_of_this_series = models.ForeignKey(Season, on_delete=models.CASCADE, default=True)
number_of_series = models.IntegerField(default=0, blank=True, null=True)
description = models.TextField(max_length=700, blank=True, default=None)
size_of_torent_file = models.CharField(max_length=60, default=None)
link_for_dowloand_serie_in_quality_360p = models.CharField(max_length=60, default=None)
link_for_dowloand_serie_in_quality_720p = models.CharField(max_length=60, default=None)
link_for_dowloand_serie_in_quality_1080p = models.CharField(max_length=60, default=None)
rating = models.FloatField(default=0, blank=True)
is_active = models.BooleanField(default=True)
timestamp_rus = models.DateField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
timestamp_eng = models.CharField(max_length=60)
time_of_series = models.DecimalField(max_digits=10, decimal_places=2, default=42)
def get_absolute_url(self):
return reverse('series:post_of_serie', kwargs=
{'serial_slug': self.serial_of_this_series.slug,
'season_slug': self.season_of_this_series.slug,
'series_slug': self.slug})
def __str__(self):
return "%s | %s" % (self.rus_name, self.number_of_series)
class Meta:
ordering = ["-timestamp_rus"]
verbose_name = 'Series'
verbose_name_plural = 'Series'
def series_change_number(sender, **kwargs):
ser = Series.objects.last()
change = ser.number_of_series
number = int(change) + 1
series = Series
series.number_of_series = number
series.save(force_update=True)
pre_save.connect(series_change_number, sender=Series)
ok do this:
def series_change_number(sender, instance, **kwargs):
ser = Series.objects.last()
change = ser.number_of_series
number = int(change) + 1
instance.number_of_series = number
pre_save.connect(series_change_number, sender=Series)
provided you are looking to update the new model object.
Please don't line up your code like that; it makes it very hard to read.
Your problem is here (removing the spaces):
series = Series
That just makes series another name for the Series class. You don't ever instantiate it; to do so you need to actually call it.
series = Series()
... assuming that is actually what you want to do; it's not clear from your code.
How can I get the foreign key values? I have a common vehicle model that links to the year, series, engine type, body style, transmission and drive train...all as foreign keys. I'd like to get the values of these fields for my app, but I'm stuck as to how I'd go about them. Any ideas will be highly appreciated.
class Model(models.Model):
model = models.CharField(max_length=15, blank=False)
manufacturer = models.ForeignKey(Manufacturer)
date_added = models.DateField()
def __unicode__(self):
name = ''+str(self.manufacturer)+" "+str(self.model)
return name
class Year(models.Model):
ALPHA_NUMERIC_CHOICES = (
('1', 'Numeric'),
('A', 'Alphabetic'),
)
year = models.PositiveSmallIntegerField()
position_7_char = models.CharField(max_length=1, choices=ALPHA_NUMERIC_CHOICES)
position_10 = models.CharField(max_length=1, blank=False)
def __unicode__(self):
return unicode(self.year)
class Series(models.Model):
series = models.CharField(max_length=20, blank=True)
model = models.ForeignKey(Model)
date_added = models.DateField()
def __unicode__(self):
name = str(self.model)+" "+str(self.series)
return name
class CommonVehicle(models.Model):
year = models.ForeignKey(Year)
series = models.ForeignKey(Series)
engine = models.ForeignKey(Engine)
body_style = models.ForeignKey(BodyStyle)
transmission = models.ForeignKey(Transmission)
drive_train = models.ForeignKey(DriveTrain)
def __unicode__(self):
name = ''+str(self.year)+" "+str(self.series)
return name
class Vehicle(models.Model):
stock_number = models.CharField(max_length=6, blank=False)
vin = models.CharField(max_length=17, blank=False)
common_vehicle = models.ForeignKey(CommonVehicle)
exterior_colour = models.ForeignKey(ExteriorColour)
interior_colour = models.ForeignKey(InteriorColour)
interior_type = models.ForeignKey(InteriorType)
odometer_unit = models.ForeignKey(OdometerUnit)
status = models.ForeignKey(Status)
odometer_reading = models.PositiveIntegerField()
selling_price = models.PositiveIntegerField()
purchase_date = models.DateField()
sales_description = models.CharField(max_length=60, blank=False)
def __unicode__(self):
return self.stock_numberodels.ForeignKey(CommonVehicle)
You need the actual IDs? Try something like my_vehicle_ref.series.id.
Also, I hope you know that the series attribute right there is really an instance of Series, so you could access any of it's properties, e.g., my_vehicle_ref.series.model.model.