Assume that there is entity Product. Relation is set as follows:
Product --1:N--> ImageLink <--N:1-- Image
Such a setup is used to allow adding more info to the relation - who linked the image, when, etc. One of the attributes of ImageLink is also isMainImage (bool). That denotes that this one particular ImageLink contains the image we want to show as main image of the product.
When I want to set main image of the product I need to set all the ImageLinks to false except for the one I want to set as main. I retrieve the links from Product - $product->getImageLinks(). That will fetch all the ImageLinks entries from DB and convert them to objects. I will traverse over all of them and set the correct value for isMainImage. Then I flush the entity manager.
Someone could have added a new image and marked it as main image in the meantime. That image was not selected from DB and was not converted to object. So is was not set to false. It will therefore not be changed during the flush.
Now I have two main images. How do I solve this?
Without ORM I would update the images in one db query, something like UPDATE image_link SET is_main = IF(id = 13, 1, 0).
Related
This is maybe a poor way to ask this question, as I haven't yet tried anything since I'm not sure if it's even possible without a bunch of custom JS added to the admin.
Given a model such as:
class MyModel(models.Model):
sku = models.CharField('SKU', max_length=20, primary_key=True)
bar_code = models.ImageField(upload_to='images/barcodes')
I want the user to select an image which will be a photo of a product's barcode. The image then needs to be processed to scan for the barcode (I'm using pyzbar) and this value should be saved to the primary key field sku.
Since I can't save the model without a primary key, and I need to upload the image field to scan the barcode to discover the value to be used for the primary key, I'm thinking that the only way to do this would be to use some client side JS in the admin to upload the image to a temp location using a DRF endpoint (or similar), read the barcode and return that value to the client which could then set the value of sku with some basic javascript. Then the model can be saved and image uploaded (a second time).
Is there a more straightforward way to do this in Django without adding my own client side JS to the admin and having to upload the photo twice?
Aside from the primary key, which may complicate things - you can extend ImageField and see how it populates its width and height columns, then do the same for the pyzbar generated barcode. See the update_dimension_fields for inspriation:
Update field's width and height fields, if defined.
This method is hooked up to model's post_init signal to update
dimensions after instantiating a model instance. However, dimensions
won't be updated if the dimensions fields are already populated. This
avoids unnecessary recalculation when loading an object from the
database.
Dimensions can be forced to update with force=True, which is how
ImageFileDescriptor.set calls this method.
You would add a method to do the work for pyzbar and then connect the signal in the same way.
To add an attribute or feature to a product by webservice, I need the id
To get the id for "red", I must first make a call to
get product_attribute_values?filter[id_category]=9 // 9 is colours
Then from that list I have to
get product_attribute_values/2
get product_attribute_values/4
get product_attribute_values/17
...
to get the names of each attribute_value (green,blue,red) until I find the value I want.
Is there no way to get attribute id by name?
I could do it easily in a join or two in mysql, but I am trying to do a clean import module to use only the webservice.
Because attributes like dimensions and weight are to be stored as an attribute or feature, even if the product schema contains dimensions and weight, the dimension and weight lists are rather large.
A work-around could be to just once scan the attribute/feature/options, and build a translate table in my import software.
Then when a product needs an attribute value, look it up in the cached table, perhaps verify if the tranlation text->id is still correct, and if not, rescan the attributes.
But can it be avoided?
In my application, the data in one model is "meta" in nature and defines the attributes in another model. Imagine retrieving a database, where the column definition data determines the attributes of each row.
I can reopen() the row model so long as I have not yet requested rows from the store. A subsequent call to store.findAll('row') retrieves data with all the attributes defined.
If however I have already called store.findAll('row') prior to Column.reopen(), then I cannot find a way cause the store to acknowledge the new attributes.
I have tried without success:
unloading all the rows and finding them again
calling peekAll/update on the rows already in the store
calling Ember.defineProperty() on the row model instead of reopen.
Both existing and subsequent new rows do not get the new attributes.
The solution that works for me is:
let store = this.get('store');
Ember.getOwner(store).unregister('model:item');
Item.reopen(newAttributes);
Ember.getOwner(store).register('model:item', Item);
let currentRoute = this.get('currentRoute');
if (currentRoute) {
currentRoute.refresh();
}
I register the route with the service that updates the schema. When the schema change is detected (which I debounce to avoid thrashing), I unregister the model, update it with reopen, re-register it and refresh the route. There may be a way to get the store to reflect these changes without refreshing the route, but I could not find it.
My question is how do i join unrelated resources that have a similar variable
Both resources have an 8 length VARCHAR variable, both named code.
Due to how to data is constructed I cannot make any assumption that would lead to this being a foreign key relation but I do however need to join this two tables together if they have similar code values, how do I join these resources together to be displayed in tastypie/django?
class CodeDescription(models.Model):
code = models.CharField(db_column='Code', max_length=10)
description = models.CharField(db_column='Description', max_length=255)
class TechnicalDif(models.Model):
code = models.CharField(db_column='Code', max_length=10)
As you can see these tables hold the same sort of value but CodeDescription holds the details of what the code means, but doesn't necessarily have the definition for all the codes, so a foreignkey relation cannot be applied, How would i join these two tables for display using them as a tastypie resource?
The only way I think you can do this is programatically in the dehydrate method of the relevant TastyPie ResourceClass. So for each loaded object you could do a query and set an appropriate value in the bundle.
Alternatively, I suppose you could create a join table, that joins related CodeDescriptions to TechnicalDifs, and populate it programatically with a query that you run over the tables intermittently (e.g. when something changes).
I am trying to create a store of products with WooCommerce and would like, if possible, to simplify the process of adding new products by having a default set of attributes.
For example, all of my products share certain attributes including: volts, watts, colour temperature etc. I have created these attributes and can select them when I add a new product but it would be really useful, for consistency and speed, if every time I added a product of a certain category the Add Product page automatically added a given set of attributes, rather than me having to add each attribute one by one for every product I add.
Does anyone know if this is possible?