Perform CRUD operations on product class? - python

I'm a newbie to Django-oscar and I'm trying to develop a simple CRUD operation on Product. I've forked the catalogue app and created a views.py file
I fired the query Product.objects.create(title='Hello') and a product does get created with the following error:
AttributeError: 'NoneType' object has no attribute 'attributes'
product_title = 'MyPhone'
upc=987654321
product_class = ProductClass.objects.get_or_create(name='Phone')
def createProduct(request):
line1
product.name = product_title
product.product_class = product_class
product.upc=upc
product.save()
When I put product=Product() in line1 I get the following error:
Cannot assign "(, False)": "Product.product_class" must be a "ProductClass" instance.
When I put product = Product.objects.create(upc=upc) I get the following error :
NoneType' object has no attribute 'attributes'
Anyone guide me on how to write a simple create operation?

ProductClass, Product, Category, ProductCategory = get_classes(
'catalogue.models', ('ProductClass', 'Product', 'Category',
'ProductCategory'))
create_from_breadcrumbs = get_class('catalogue.categories', 'create_from_breadcrumbs')
def _create_item(product_class, category_str, upc, title,
description, stats):
# Ignore any entries that are NULL
if description == 'NULL':
description = ''
# Create item class and item
product_class, __ = ProductClass.objects.get_or_create(name=product_class)
try:
item = Product.objects.get(upc=upc)
stats['updated_items'] += 1
except Product.DoesNotExist:
item = Product()
stats['new_items'] += 1
item.upc = upc
item.title = title
item.description = description
item.product_class = product_class
item.save()
# Category
cat = create_from_breadcrumbs(category_str)
ProductCategory.objects.update_or_create(product=item, category=cat)
return item
This is the actual way to manipulate products with provided information to the function in django oscar. For better design decisions you need to follow this convention. Modify it as you want. Let me know if you want more help. Thank you.

Related

Django modify objects using key=value options

I'm doing http POST processing of incoming data modifications requests (from a DataTables table) for objects in my Django environment. The POST data comes to me in key=value pairs.
The problem I'm running into is that I haven't found a way to turn this into something I can use. Django objects and modifications are done like this:
id = 123
item = Stuff.objects.get(id=id)
item.title = "New Title"
item.save()
I have the following data attributes:
id = 123
attribute = "title"
value = "New Title"
How can I use them to modify a Django object?
A more efficient way to do this is with:
id = 123
attribute = 'title'
value = 'New Title'
Stuff.objects.filter(id=id).update(**{attribute: value})
This will prevent first fetching the object with a query, and then update it.
If you need to load the object anyway, you can work with setattr(…) [Python-doc]:
id = 123
attribute = 'title'
value = 'New Title'
item = Stuff.objects.get(id=id)
setattr(item, attribute, value)
item.save()
Try using an if statement like
If attribute = 'title'
item.title = value
item.save()

Loop through record lines method - Odoo v8

I have this method:
#api.onchange('qty', 'consumed_qty')
def _remaining_func(self):
for qty in self.isbn:
if self.qty or self.consumed_qty:
self.remaining_qty = self.qty +(-self.consumed_qty)
But I need it to loop through records of my line (One2many field), right now, if I add just one record it works just fine, but if I add two or more, it throws the Expected singleton error.
So, how can I loop with this method?
I have added the for qty in self.isbn but with no success.
This is the class where this method is declared:
class bsi_production_order_lines(models.Model):
_name = 'bsi.production.order.lines'
production_order = fields.Many2one('bsi.production.order', string="Production Orders")
isbn = fields.Many2one('product.product', string="ISBN", domain="[('is_isbn', '=', True)]")
qty = fields.Float(string="Quantity")
consumed_qty = fields.Float(string="Consumed quantity")
remaining_qty = fields.Float(string="Remaining quantity", compute="_remaining_func")
Any ideas?
you could try:
#api.onchange('qty', 'consumed_qty')
def _remaining_func(self):
for s in self:
for qty in s.isbn:
if s.qty or s.consumed_qty:
s.remaining_qty = s.qty +(-s.consumed_qty)
I hope this help you.

How to correctly Update Odoo / Openerp website context?

I trying to adapt the module https://www.odoo.com/apps/modules/9.0/website_sale_product_brand/ to have a select box on the shop page, and filter by brand and category, and not to have to go to a diferent page and select the brand.
In that module they update context with the brand_id so the sale_product_domain function could append to the domain. In the module, it filter it as a charm, but in my code not....
Any guest?
When I debug self.env.context in the sale_product_domain function not brand if append, but in the website_sale_product_brand yes, with the exactly same code
controller.py
class WebsiteSale(website_sale):
#http.route(['/shop',
'/shop/page/<int:page>',
'/shop/category/<model("product.public.category"):category>',
'/shop/category/<model("product.public.category"):category>/page/<int:page>',
],type='http',auth='public',website=True)
def shop(self, page=0, category=None, search='', brand=None, **post):
# Update context to modify sale_product_domain function from website model
if brand:
context = dict(request.env.context)
context.setdefault('brand', int(brand))
request.env.context = context
result = super(WebsiteSale, self).shop(page=page, category=category,
brand=brand, search=search,
**post)
#append brand to keep so links mantain brand filter
keep = QueryURL('/shop',
brand=brand,
category=category and int(category),
search=search,)
#attrib=attrib_list TODO
#Update result
result.qcontext['keep'] = keep
result.qcontext['brands'] = http.request.env['product.brand'].search([]) #use to populate template select box
result.qcontext['sel_brand_id'] = brand #use to select the selected brand on brand select box
return result
models.py
class WebSite(models.Model):
_inherit = 'website'
#api.multi
def sale_product_domain(self):
domain = super(WebSite, self).sale_product_domain()
print self.env.context
if 'brand' in self.env.context:
domain.append(
('product_brand_id', '=', self.env.context['brand']))
return domain
ctx = dict (request.context)
ctx.update ({'bin_size': True})
request.context = ctx
That's it!

AttributeError: 'list' object has no attribute 'objects'

I am trying to run the following which is throwing an
AttributeError: 'list' object has no attribute 'objects'
script.py
#Get Dota2 Item Rarities
dotaItemRarityUrl = 'http://api.steampowered.com/IEconDOTA2_570/GetRarities/v1?'
dotaItemRarityPayload = {'key': settings.SOCIAL_AUTH_STEAM_API_KEY,
'language': 'en',
}
dotaItemRarityInfo = requests.get(dotaItemRarityUrl, params=dotaItemRarityPayload)
dotaItemRarity = dotaItemRarityInfo.json()
dotaItemRarity = dotaItemRarity['result']['rarities']
print(dotaItemRarity)
#print(dotaItemQualities)
#Populate Database With Item Rarities that do NOT exist already
for rarity in dotaItemRarity:
print rarity
irarityId = rarity['id']
irarityProperName = rarity['localized_name']
irarityInternalName = rarity['name']
irarityColor = rarity['color']
q = dotaItemRarity.objects.filter(rarityId=irarityId)
print q
if len(q) == 0:
newRarity = dotaItemRarity(rarityId=irarityId,
rarityProperName=irarityProperName,
rarityInternalName=irarityInternalName,
rarityColor=irarityColor)
newRarity.save()
models.py
class dotaItemRarity(models.Model):
rarityId = models.IntegerField(max_length=3,primary_key=True)
rarityProperName = models.CharField(max_length=60)
rarityInternalName = models.CharField(max_length=50)
rarityColor = models.CharField(max_length=30)
def __unicode__(self):
return self.rarityInternalName
I am using south to handle migrations and have tried multiple options to fix this e.g. removing the tables and rebuilt them. As far as I can tell this should work, can anyone point me in the right direction.
dotaItemRarity is a list, and it has no objects attribute:
q = dotaItemRarity.objects.filter(rarityId=irarityId)
That's because you bound it to a list from your JSON result:
dotaItemRarity = dotaItemRarityInfo.json()
dotaItemRarity = dotaItemRarity['result']['rarities']
It is not a Django model, as you appear to expect it to be.
If you had the dotaItemRarity Django model imported into script.py, then the name is no longer bound to that model, as you replaced it with the list.
Rename the list to use a different name that doesn't mask the model.
Note that the Python style guide recommends that you use CamelCase names for classes (including Django models), to avoid such mistakes.
Following PEP 8 to refactor your code a little, as well as using some clearer naming an practices:
models.py:
class DotaItemRarity(models.Model):
rarity_id = models.IntegerField(max_length=3, primary_key=True)
rarity_proper_name = models.CharField(max_length=60)
rarity_internal_name = models.CharField(max_length=50)
rarity_color = models.CharField(max_length=30)
def __unicode__(self):
return self.rarity_internal_name
script.py:
#Get Dota2 Item Rarities
url = 'http://api.steampowered.com/IEconDOTA2_570/GetRarities/v1'
payload = {'key': settings.SOCIAL_AUTH_STEAM_API_KEY, 'language': 'en'}
response = requests.get(url, params=payload)
rarities = response.json()['result']['rarities']
for rarity in rarities:
rarity_id = rarity['id']
try:
DotaItemRarity.get(rarity_id=rarity_id)
except DotaItemRarity.DoesNotExist:
new_rarity = DotaItemRarity(
rarityId=rarity_id,
rarity_proper_name=rarity['localized_name'],
rarity_internal_name=rarity['name'],
rarity_color=rarity['color'])
new_rarity.save()

Python populating a database with one-to-many relationship

I'm a beginner, so please go easy on me. I am working on a script so that I don't have to keep entering in data when I decide to drop the database. My entire script works well, except when I'm dealing with a one-to-many relationship. It will not save to the database. Can anyone tell me what I am doing wrong or point me in the right direction?
SCRIPT:
try:
pmod.Instrument.objects.get(title='kjkjsdfsadfs')
except pmod.Instrument.DoesNotExist:
u = pmod.Instrument()
u.title = 'Bach 42 Trombone'
u.price = 550.00
u.soundDescription = 'Good'
u.functionalityDescription = 'Good'
u.damageDescription = 'Good'
u.accessoryDescription = 'Good'
u.customerName = 'Jake'
u.customerEmail = 'ks#gmail.com'
u.instrumentCategory = 1
print('Good2')
u.save()
print('Instrument1 saved')
MODEL:
class Category(models.Model):
instrumentCategory=models.CharField(max_length=50,blank=True,null=True)
def __str__(self):
return self.instrumentCategory
class Instrument(models.Model):
title = models.CharField(help_text='title',max_length=50,blank=True,null=True)
price = models.DecimalField(max_digits=8, decimal_places=2)
soundDescription=models.CharField(max_length=1000,blank=True,null=True)
functionalityDescription=models.CharField(max_length=1000,blank=True,null=True)
damageDescription=models.CharField(max_length=1000,blank=True,null=True)
accessoryDescription=models.CharField(max_length=1000,blank=True,null=True)
customerName=models.CharField(max_length=50,blank=True,null=True)
customerEmail=models.EmailField(max_length=254,help_text='Enter valid email address')
instrumentCategory=models.ForeignKey(Category)
u.instrumentCategory = 1
That's not how a models.ForeignKey field works in Django. You need to get an instance of the Category object and assign that to u.instrumentCategory.
u.instrumentCategory = pmod.Category.objects.get(id=1)
You may try :
u.instrumentCategory_id = 1

Categories