JMS serializer loading to much - doctrine-orm

I'm working with Symfony3 and JMS serializer 1.1. I want to serialize Product Object that have several relations:
Product
Images[]
Recommended[
Product
Images[]
Recommended[]
Product
Images[]
Recommended[]
]
I want to get all recommended for products, but for recommended I want to get only images, not their recommended. So recommended products should appear only for main product. I tried with MaxDepth() but it only allowed me for "cutting" images and recommended for recommended (both are empty). It would be great if there was a way for applying one Group for first level products and another for second level products. The problem is Doctrine loads all the recommended for the second level and I just don't need that.

Related

Suggestions for splitting up a Django model of 1000+ fields

I am looking for suggestions on how to deal with a database table that has 1000 or more columns and I am tying to translate it into one or many Django models. The data also needs to be filtered through API calls within the URL. Every field needs to be able to filter the rest of the data.
I have come up with a few solutions and would like input or resources related to them:
Just have a model with 1000+ fields - This seems like it would be a nightmare to maintain and would require a lot of brute force coding but would likely work fine if data was selectively returned.
Use a JSON field to store all less frequently accessed data - The issue here would be difficulty in filtering the data using Django Filters.
Split data into related models connected by One-to-One relationships, as I understand this cuts down on join operations. - This would seem to require more coding than the first option but would be more maintainable.
Does anyone have any information or resources on dealing with database tables of this size?
You should absolutely split the model into multiple linked models.
Because of how Django models data in the database you should generally have a 1:1 relationship between models and tables, and your tables should be normalized to at least the third normal form.

How to add recommended products in django oscar dashboard

I am using django-oscar == 1.6.1 for a project.
I am trying to add recommended products to an individual product in the dashboard, currently I see that the recommended product field is empty, how do I populate it and give the ranking?
It's a streaming search field, whatever you type should search for related term in your existing products database.
For example, if you type <search_term> it would ultimately query (after several intermediate queries of substrings) & hit http://localhost:8000/dashboard/catalogue/product-lookup/?q=<search_term>, the view for which can be found here. As you can see, it searches the product titles only, if you need something else, you can always modify it.
By the looks of it, you haven't populated your products database yet, or there's something else wrong with your installation or setup.

Connecting Two Django Apps, but Keeping them Separated

I have two distinct Django Apps, each one completely working separated. Those Apps are "PointOfSale" and "Inventory". What occurs is that both of these Apps have a "Product" table, in which the products are inserted. Now, keeping those two tables filled with basic the same data is obvious redundant (actually, the Inventory table of Products have more fields).
I'm now contemplating the different merge strategies to deal with this issue and I would like some help. Ideally, I would like to keep the possibility of each App working independently. So, I envisioned the following scenarios:
A Save Signal that keep the two tables insync
The connection of the two with OneToOne connection
Generic Relationship
The creation of a third App, called Products, in which the tables Products and CategoryOfProducts are kept, and both the Apps PointOfSale and Inventory uses it as a prerequisite.
MErge everything in one big App.
Another that I couldn't think of.
Thanks
I think the best way is to use the same database table from both Product models. See how you can have multiple databases in apps: https://docs.djangoproject.com/en/1.10/topics/db/multi-db/#an-example
So say AppA uses DB_A and AppB uses DB_B as databases for their unique models and that Product model/table is in DB_A. You can configure AppB to use DB_A for Product model while keeping DB_B for other models.
Ideally, if the models are the same, you can package Product app so that the same code is used in all projects.

Django Copy Related Data and keep them unchanged over time

Using ForeignKey relationships, I want to be able to copy data and store it in another model. For example, think of how you would handle past Invoices and billed Services.
The Invoice would have one or more Services associated with it and with prices for the Services. This prices for a Service can / will change over time - but the Service price recorded with the Invoice should remain as it was when the Invoice was created.
My first thought was to create a pdf from the resulting data and store it. But this would make the data somewhat inaccessible.
Is there somehow a way to copy the data and keep them accessible?
This is a pretty broad problem with multiple solutions. I dont think that what you're aiming to is the correct one.
One rule for saving invoices is, that invoices never change. You should never update an invoice. So not only your 'copies' of invoices should remain the same, but the original too.
Also, you should have a InvoiceItem (or InvoiceRow) model which are the items on your invoice. Don't bind Products to a Invoice directly.
Here are 2 solutions I've used:
Solution 1
You can normalize the data on your invoice(items). So, don't use foreignkeys, but normalize all data about the product, so product info (incl. price) is saved within the invoice(item).
Solution 2
Give your products revision numbers. So everytime a product is updated (name or price change for example), a new product is created in the database. Now you can link the InvoiceItem to a Product with a Foreign Key, and it will will be historically accurate.
Im sure there are some guides/best practices for creating Invoice backends. Language or Framework is not important. Invoicing is really important, so do alot of research before starting to build something. That's just my two pennies

How to know where database has changed

I have a project that looks like a simple shopping site that sells different kinds of products. For example, I have 4 models: Brand, Product, Consignment. Consignment is linked to Product, and Product is linked to Brand. To reduce count of queries to databases, I want to save current state of these models(or at least some of them). I want to do it, because I show a sidebar with brands and products. So every time when user opens some page, it will execute the query to database to get those brands and products.
But when admin add some new product or brand, I want to handle database changing and resave it. How to implement it?
Your answer is by using Cache. Cache is a method to store your objects in memory/other app like redis temporarily so that you do not need send queries to database. You can read the full description here.
Or, you can use this third party library that helps you to cache Django ORM Model. Here are the example.
Brand.objects.filter(name='stackoverlow').cache()
After doing an update to the model, you need to clear or invalidate the cache.
invalidate_model(Brand)