i am getting 'object' has no attribute 'objects'. Here is my code from views.py
def product_list(request, category_slug=None):
category = None
categories = category.objects.all()
products = Product.object.filter(available=True)
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = products.filter(category=category)
return render(request,'shop/prod/list.html' , {'category':category,
'categories': categories,
'products': products
})
def product_detail(request, id, slug):
product = get_object_or_404(Product, id=id, slug=slug, available=True)
cart_product_form = CartAddProductForm()
return render(request,
'shop/prod/detail.html',
{'product': product,
'cart_product_form':cart_product_form })
category = None
categories = category.objects.all()
You are setting category to None then immediately setting categories to a property of category, which can only be an error because None in python is the absence of a value.
You can check out more information on the official python docs here:
https://docs.python.org/3/library/constants.html#None
Related
I have checked a few of the posts having the same issue and it tells me how to do it but it is not working.
My code:
class Product:
def __init__(self,price,prod_id,quantity):
self.price = price
self.prod_id = prod_id
self.quantity = quantity
self.shop_inventory = {}
self.amount = {}
class Inventory(Product):
def update_stock(self):
self.shop_inventory[self.prod_id] = self.quantity
self.amount[self.prod_id] = self.price
def return_stock_item(self):
look_up = input("What item would you like to check? ")
item = self.shop_inventory[look_up.lower()]
price = self.amount[look_up.lower()]
return f"{look_up.capitalize()}: There are {item} in stock and cost ${price} each."
def display_stock(self):
return self.amount.items()
To set the values Ive been using
stock = Inventory(2.34,'apple',5)
stock.update_stock()
It's part of a class and everything else in the class is working right. Just this one function is not working. I have tried this method as well as the update() method and neither are adding new key values. It is only over writing the same first spot. I have an item check in the class for all the dictionary items and it always only returns one [key][value] no matter how many times I change the key name.
Thank you for any help.
I have a simple case class MyContext(queries: Query) that I provide to the schema with : sangria.schema.Schema(deriveContextObjectType[MyContext, Query, Unit](_.queries)
MyQuery is a trait of Query
trait MyQuery {
#GraphQLField
def item(ctx: Context[MyContext, Unit])(id: String) ...
}
This works great. But what if I want to nest resolvers?
query {
item {
status # status is resolved from source B
price # price is resolved from source C
}
}
Is that possible to achieve? Would I return an ObjectType[Item] that has properties status and price annotated with #GraphQLField?
I think you can use deriveObjectType for an Item. It is also able to handle the #GraphQLField annotation (as an alternative you can also use IncludeMethods macro setting). Here is an example:
implicit val ItemType = deriveObjectType[MyContext, Item]()
Really appreciate #tenshi's answer! I tried using the deriveObjectType but was getting a type error:
type mismatch;
found : sangria.schema.Context[MyContext,Item]
required: sangria.schema.Context[MyContext,Unit]
But using deriveContextObjectType in conjunction with AddFields is working:
def schema = sangria.schema.Schema(
deriveContextObjectType[MyContext, Query, Unit](_.queries,
AddFields(
Field(
name = "item",
fieldType = deriveContextObjectType[MyContext, Item, Unit](_ => new Item),
resolve = _ => ()
)
))
)
And the Schema looks good, yielding:
type Query {
item: Item!
}
I have a category named Z and a subcategory named A.
In my function, I am using this to get category Z (Parent)
$category[0]->slug
The problem is that subcategory A is set as [0] because it is alphabetical order. Z is the parent category, so Z should be [0]. I don't get why it is in this way.
I recommend to use this wordpress function to do that:
//Load data for your Sub-Category
$subcategory = get_category("A");
//from Sub-Category get parent ID ( Z )
$parent_category_ID = $subcategory->parent;
//load object for Parent-Category ( Z )
$objet_parent_category = get_category($parent_category);
//Get the Parent Category slug
$parent_name = $parent_name->slug; // to get name: ->name
I hope help you!
I am trying to make ACF data available on child pages two levels under the parent. I have a solution for making it available to a child page:
if ( $post->post_parent ) {
$headingFont = get_field('community_heading_font', $post->post_parent);
$bodyFont = get_field('community_body_font', $post->post_parent);
$primaryColor = get_field('community_primary', $post->post_parent);
$secondaryColor = get_field('community_secondary', $post->post_parent);
$fifteenSecondaryColor = get_field('community_fifteen_secondary', $post->post_parent);
$tertiaryColor = get_field('community_tertiary', $post->post_parent);
}
However, this information isn't available once we're a level deeper. That is, the ACF field 'community_heading_font' isn't available to the grandchild of the page originally providing data for that field.
I've tried post->post_parent->post_parent, and I've also tried to use post->post_parent on a variable:
$parentPage = $post->post_parent;
$grandparentPage = $parentPage->post_parent
To get the $grandparentPage ID for use in your ACF functions, use the wp_get_post_parent_id() function.
$grandparentPage = wp_get_post_parent_id($post->post_parent);
$headingFont = get_field('community_heading_font', $grandparentPage);
https://codex.wordpress.org/Function_Reference/wp_get_post_parent_id
I am trying to assign a Channel to my products created programmatically, so they will be displayed on shop. Unfortunately, there is no documentation for this use case.
Here is how I create my products:
$productFactory = $this->container->get('sylius.factory.product');
$productManager = $this->container->get('sylius.manager.product');
$productRepository = $this->get('sylius.repository.product');
$productVariantFactory = $this->get('sylius.factory.product_variant');
$productVariantRepository = $this->get('sylius.repository.product_variant');
$channelPricingFactory = $this->get('sylius.factory.channel_pricing');
$channelPricingRepository = $this->get('sylius.repository.channel_pricing');
//CREATE PRODUCT
$product = $factory->createNew();
$product->setName('TEST 2 - '.$title);
$product->setCode($this->generateRandomString());
$product->setSlug($this->generateRandomString());
$productRepository->add($product);
//CREATE VARIANT & ATTACH IT TO PRODUCT
$variant = $productVariantFactory->createNew();
$variant->setName('TEST 2 - '.$title);
$variant->setCode($this->generateRandomString());
$variant->setProduct($product);
$productVariantRepository->add($variant);
//CREATE PRICE & ATTACH IT TO VARIANT
$channelPricing = $channelPricingFactory->createNew();
$channelPricing->setPrice(999);
$channelPricing->setOriginalPrice(999);
$channelPricing->setChannelCode('US_WEB');
$channelPricing->setProductVariant($variant);
$channelPricingRepository->add($channelPricing);
Unfortunately, the products are not linked to a Channel:
The answer to your question:
You have one product variant, but Product variant implements ProductVariantInterface which extends TranslatableInterface. So we need one translation to make it appear and work. To add the translation:
$productVariant->setCurrentLocale('fr_FR');
$productVariantTranslation = $productVariant->getTranslation();
or
$productVariantTranslation = $productVariant->getTranslation($locale);
after you can add a name or not:
$productVariantTranslation->setName('What a product variant!');
after:
$product->addVariant($productVariant);
$this->entityManager->persist($product);
You will have your product variant on line.
Another thing is that:
$channelPricingRepository = $this->get('sylius.repository.channel_pricing');
$channelPricingRepository->add($entity);
it's going to flush directly data to DB in each call. In the example you are going to flush 3 times instead only one. In bigger proccess with many "add" this can be a lack of performance. You can simply;
$this->entityManager->persist($entity); //Many times
$this->entityManager->flush();
Here is how I managed to create my products (to attach them then to my entities):
function createProduct($livre,$options){
$title = $livre['title'];
$prix = $livre['prix'];
// SYLIUS REPOSITORIES LOAD
$productFactory = $options['sylius_factory_product'];
$productRepository = $options['sylius_repository_product'];
$productVariantFactory = $options['sylius_factory_product_variant'];
$productVariantRepository = $options['sylius_repository_product_variant'];
$channelPricingFactory = $options['sylius_factory_channel_pricing'];
$channelPricingRepository = $options['sylius_repository_channel_pricing'];
//CREATE PRODUCT
$product = $productFactory->createNew();
$product->setName($title);
$product->setCode($title.'_'.$this->generateRandomString());
$product->setSlug($title.'_'.$this->generateRandomString());
$productRepository->add($product);
//CREATE VARIANT & ATTACH IT TO PRODUCT
$variant = $productVariantFactory->createNew();
$variant->setName($title. 'Variant');
$variant->setCode($title.'_'.$this->generateRandomString());
$variant->setProduct($product);
$productVariantRepository->add($variant);
//CREATE PRICE & ATTACH IT TO VARIANT
$channelPricing = $channelPricingFactory->createNew();
$channelPricing->setPrice($prix*100);
$channelPricing->setOriginalPrice($prix*100);
$channelPricing->setChannelCode('CH');
$channelPricing->setProductVariant($variant);
$channelPricingRepository->add($channelPricing);
$productId = $product->getId();
return $productId;
}
I added the channel to the product like this:
$channelCode = 'US_WEB'
$channelRepository = $this->get('sylius.repository.channel');
$channel = $channelRepository->findOneBy(array('code' => $channelCode));
$product->addChannel($channel);