This does not seem difficult, but I am stacked here.
I want to reference table column by variable name for filtering and cannot understand why this is not working.
let
Source = Table.FromRecords({
[VarColumn = "Val", Phone = "123-4567"],
[VarColumn = "Val2", Phone = "987-6543"],
[VarColumn = "NA", Phone = "543-7890"],
[VarColumn = "OTHER", Phone = "543-7890"]
}),
VariableColumn = "VarColumn",
filter = Table.SelectRows(Source, each (Table.Column(Source, VariableColumn) = "OTHER"))
in
filter
Try
filter = Table.SelectRows(Source, each Record.Field(_, VariableColumn) = "OTHER")
Related
My models: https://pastebin.com/qCMypxwz
How can I query the Variants table from Products table? I want to get the values of image and colors (which is a table itself) from the Variants table. This is what I am doing so far but this is giving me similar queries according to debug tool:
productList = Products.objects.prefetch_related('category', 'variants_set__color_id', 'variants_set__image')
for productName in productList:
products = dict()
prod_id = productName.variants_set.all()[0].id
products['id'] = prod_id
products['category'] = productName.category.category_name
products['prod_name'] = productName.prod_name
products['brand'] = productName.brand
prod_colors = productName.variants_set.all().values_list('color_id__name', flat=True).distinct()
prod_images = list(productName.variants_set.all()[0].image.all())
image_list = list()
for image in prod_images:
image_list.append(image.image_url)
products['image'] = image_list
products['colors'] = list(prod_colors)
price = productName.variants_set.all()[0].price
products['price'] = price
createdAt = productName.variants_set.all()[0].createdAt
products['createdAt'] = createdAt
productListDict.append(products)
I am trying to get a list of the sum of the count of different choices. The choices are strings, so I want to count the same strings and get their sum, and then do the same for other choices. I made a query but it does not give me the total count of each choice instead list them all with the count 1.
model.py
class sublist(models.Model):
Music = 'Music'
Video = 'Video'
Gaming = 'Gaming'
News = 'News'
Lifestyle = 'Lifestyle'
Access = 'Access'
SUBTYPE_CHOICES =(
(Music , "Music"),
(Video , "Video"),
(Gaming , "Gaming"),
(News , "News"),
(Lifestyle , "Lifestyle"),
(Access , "Online Access"),
)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,)
name = models.CharField(max_length=150)
cost = models.FloatField(default = 0)
subtype = models.CharField(max_length= 50, choices = SUBTYPE_CHOICES, default = Access)
This is my query. (i tried coming up with other combinations by looking at the documentation but no luck)
expenselist = sublist.objects.filter(author = curruser.id)
subtypecount = list((expenselist
.annotate(subcount=Count('subtype'))
.values('subtype', 'subcount')
))
result of query above: [{'subtype': 'Access', 'subcount': 1}, {'subtype': 'Access', 'subcount': 1},{'subtype': 'Video', 'subcount': 1}]
Desired result: [{'subtype': 'Access', 'subcount': 2},{'subtype': 'Video', 'subcount': 1}]
Here is the query that will produce your desired result.
First use .values() then .annotate()
subtypecount = sublist.objects.filter(author = curruser.id).values('subtype').annotate(subcount=Count('subtype'))
or
expenselist = sublist.objects.filter(author = curruser.id)
subtypecount = list(expenselist.values('subtype').annotate(subcount=Count('subtype')))
My API is returning lists with three entries like so (pseudocode only)
results=[]
for db_row in db_rows:
result = []
result.append (db_row[0]) # Name (string)
result.append (db_row[1]) # Age (int)
result.append (db_row[2]) # DOB (timestamp)
results.append (results)
I want to create a model for it to show up in swagger docs like so
_results_model = api.model (
'results model',{
'results': fields.List(
fields.String(
description = 'Name of user',
min_length = 6,
max_length = 64,
pattern = '.*',
help = "...",
example = 'john.smith'
),
fields.Integer(
description = 'Age of user',
minimum=0,
help = "...",
example = '25'
),
fields.DateTime(
description = 'Date of birth',
help = "...",
example = '1918-11-11'
)
)
}
)
But it appears it is not possible to pass more than one parameter to fields.List
'results': fields.List
TypeError: __init__() takes 2 positional arguments but 4 were given
Question: What other option do I have for creating a model to represent a complex list?
I have a One2many field in inventory for product pack. And I want that all fields values of that One2many field should update in fields of One2many field in sales order, row and column wise. So, Anyone can help me.
My Python code is here:
class ProductTemplate(models.Model):
_inherit = "product.template"
product_pack = fields.One2many('product.pack', 'template_id', string='Product Pack', copy=True)
class ProductDesign(models.Model):
_description = 'Product Pack'
_name = "product.pack"
_rec_name = "product_id"
check_option = fields.Boolean('#')
template_id = fields.Many2one('product.template', string='Template', required="True", ondelete='cascade', index=True, copy=True)
product_id = fields.Many2one('product.product', string='Product', required="True", domain="[('is_pack','=',False)]")
services = fields.Many2one('gold.service')
qty = fields.Integer('Quantity', default=1)
Here in 'wizard' (One2many field table) i want to update all fields (row and column) of product_pack
class SalePackWizard(models.TransientModel):
_name = "sale.pack.wizard"
_description = "Sale Pack Wizard"
product_id = fields.Many2one('product.product', string="Product Pack", required=True, domain="[('is_pack','=',True)]")
wizard = fields.One2many('product.gold','service')
#api.onchange('product_id')
def _onchange_product_pack_name(self):
for w in self.product_id.product_pack:
for s in w:
print "s:", s.product_id.name, s.services, s.qty
r = []
print"r:", r
class ProductDesign(models.Model):
_description = 'Product Pack'
_name = "product.gold"
_rec_name = "products_ids"
service= fields.Many2one('product.val', string='Templates', required="True", ondelete='cascade', index=True, copy=True)
check_box = fields.Boolean('#')
products_ids = fields.Many2one('product.product', string='Product', required="True", domain="[('is_pack','=',False)]")
services = fields.Many2one('gold.service')
qtyy = fields.Integer('Quantity', default=1)
Problem Solved.
Here is the function code of problem:
wizard = fields.One2many('product.gold','service',change_default=True, default=_onchange_action_product_add)
#api.onchange('product_id')
def _onchange_action_product_add(self):
res = self.product_id.product_pack
r = []
value = {}
for var in self.product_id.product_pack:
print "var:::", var
for line in self.product_id.product_pack:
print "line:::", line , line.product_id, line.product_id.name, line.qty, line.services, line.id
data = {'products_ids': line.product_id.id,
'service':var.id, #many2one child field from one2many field
'services':line.services,
'qtyy': line.qty
}
print "data:", data
r.append((0, 0, data))
print "r.append", r.append, r
#return data
value.update(wizard=r)
return {'value': value}
I've 3 models.
class ShipmentWeightMapping(models.Model):
id = models.IntegerField(primary_key = True)
weight = models.CharField(max_length = 255)
status = models.CharField(max_length = 255)
time = models.DateTimeField(auto_now = True, auto_now_add = True)
shipment_id = models.ForeignKey('Shipment')
class ShipmentDimensionMapping(models.Model):
id = models.IntegerField(primary_key = True)
status = models.CharField(max_length = 255)
time = models.DateTimeField(auto_now = True, auto_now_add = True)
length = models.IntegerField()
breadth = models.IntegerField()
height = models.IntegerField()
shipment_id = models.ForeignKey('Shipment')
class Shipment(models.Model):
id = models.IntegerField(primary_key = True)
job_id = models.CharField(max_length = 255)
weights = models.ForeignKey('ShipmentWeightMapping')#backref
dimensions = models.ForeignKey('ShipmentDimensionMapping')#backref
time = models.DateTimeField(auto_now = True, auto_now_add = True, db_index = True)
I want to backref weights and dimensions to their respective classes, so that when I query Shipment model for id=1, I should get length, breadth and height from ShipmentDimensionMapping and weight from ShipmentWeightMapping without querying ShipmentDimensionMapping and ShipmentWeightMapping separately.
For eg:- Currently I do like this.
To get Shipment Details for id = 1, I do the following.
dimension_obj = ShipmentDimensionMapping.objects.filter(shipment_id = 1)[0]
length = dimension_obj.length
#similarly for other details in ShipmentDimensionMapping
weight_obj = ShipmentWeightMapping.objects.filter(shipment_id = 1)[0]
weight = weight_obj.weight
#similarly for other details in ShipmentWeightMapping
shipment_obj = Shipment.objects.filter(shipment_id = 1)[0]
job_id = shipment_obj.job_id
#similarly for other details in Shipment
Is there any way in which I only query shipment_obj and I get details of ShipmentWeightMapping and ShipmentDimensionMapping?
Also, I always use the result at zeroth index [0] to get the result. Although the result returned always contains only 1 item, still I need to do [0]. How can I avoid this as well?
Since weight and dimension data are on other tables, you can't get them without querying these two tables.
For the second question, if you know there's only one entry, you can do:
shipment_obj = Shipment.objects.get(id=1)
BTW; you don't have to add ID values explicitly since they are called id in your code as well which is by default in Django.