Refactoring Entities with common fields but with different #JoinColumn parameters - jpa-2.0

I have several entities that have same fields which are common to many tables. But those fields are not linked with the same parameter name in the relational tables(see object_1 and object_2 in the attached image).
I creacted an abstract class (IGlobalObject) and I used #MappedSuperclass to link it to subclass. Now I have to override parameters but I don't know how to do it ?
Please see the attached image for more details .
Thanks in advance

Related

should I use abstract model in this situation?

I'm wondering what is the best way to represent the models, because there are some fields in common and others that change according to the category.
Example: Common fields: (title, author, content, photo, references)
Category and its specific fields:
Biography (birth data, death data, occupation)
Commemorative date (data, description)
Music (artist, album, year, musical style)
The best would be to create a generic model with common fields and others inherit from it (abstract models)?
Create a model for each category, ie repeating the common fields?
Create a single model with the common fields and have a category field that when selected would display the category-specific fields in django-admin? In that case, I suppose it would be done with jquery? Any references? There is nothing the material has on this on the internet.
Note: All user interaction to register is in django-admin.
From the docs:
Often, you will just want to use the parent class to hold information that you don’t want to have to type out for each child
model. This class isn’t going to ever be used in isolation, so
Abstract base classes are what you’re after.
If you’re subclassing an existing model (perhaps something from another application entirely) and want each model to have its own
database table, Multi-table inheritance is the way to go.
So if you don't want the parent class to have it's own table, abstract model is the way to go.

django multi-table inheritance, access method from child from instance of parent

I am using Multi-Table inheritance (aka Concrete Inheritance), where I have a non-abstract model + DB Table called Clients, which is concerned with common details concerning all the clients.
But a client can be an Individual, Partnership or Company, for which I have created inheriting models and tables. An Individual has first name + last name, and company has other specific particulars, etc.
I want to be able to access the names of clients (derived from the columns from the child tables) when I want a list of all clients.
After lot of searching, I found that this tutorial, works successfully.
Basically, it involves inserting a column on the Client table, which will store the name of the Child model. Then using that name, the appropriate child model is identified and appropriate child method is accessed.
But it seems to be a slightly cumbersome way to implement polymorphism in Multi-Table inheritance.
I want to know whether since 2012, Django has introduced any better way to deal with the issue, or is this still the only way?
Please let me know if my code sample is required, but the link provided has a beautiful example already.
There is django-model-utils application with Inheritance Manager. It will cast automatically your parent class to children instances. Example from docs:
from model_utils.managers import InheritanceManager
class Place(models.Model):
# ...
objects = InheritanceManager()
class Restaurant(Place):
# ...
class Bar(Place):
# ...
nearby_places = Place.objects.filter(location='here').select_subclasses()
for place in nearby_places:
# "place" will automatically be an instance of Place, Restaurant, or Bar
Also check this question for generic solution with ContentType.
And also check awesome article by Jeff Elmore about this topic. Quite old, but still great.

Django dynamic models

I'm pretty new to Django and Python. I'm trying to create some "category-dependent" models.
I have a Product model and i want to have category-dependent atributes. Example:
If i select "Permanent Dyes" in the category of my product i want to have specific atributes for my user to fill.
I don't want to create different models for every type of product i'm going to manage.
Is there any workaround to do this and keep using the django-admin?
Thanks in advance!
Model inheritance seems like it would help in this situation. Using it, you can have an abstract Product base class and then use Meta to add different product-dependent characteristics you need. It may require different models, but you would only need to add the attributes you needed, like below
class Product(models.Model):
....
class ProductA(Product):
class Meta:
....
This will allow you to have general properties in the Product class, such as price, etc., but use the subclass as a way to distinguish between different products.
Hope that helped in some way!

Doctrine2 Base entity class association mapping

I have a Base entity and it's children, for example: news, discounts, gallery, etc..
What I would like to achieve is, that the Base class has an association map to the Image entity, so all it's children are automatically joined with the Image class.
Could you please give me some advice how to achieve this? Thanks
Whenever you have an inheritance using Doctrine, you need to follow the guidelines:
http://doctrine-orm.readthedocs.org/en/2.0.x/reference/inheritance-mapping.html

How do I implement a common interface for Django related object sets?

Here's the deal:
I got two db models, let's say ShoppingCart and Order. Following the DRY principle I'd like to extract some common props/methods into a shared interface ItemContainer.
Everything went fine till I came across the _flush() method which mainly performs a delete on a related object set.
class Order(models.Model, interface.ItemContainer):
# ...
def _flush(self):
# ...
self.orderitem_set.all().delete()
So the question is: how do I dynamically know wheter it is orderitem_set or shoppingcartitem_set?
First, here are two Django snippets that should be exactly what you're looking for:
Model inheritance with content type and inheritance-aware manager
ParentModel and ChildManager for Model Inheritance
Second, you might want to re-think your design and switch to the django.contrib content types framework which has a simple .model_class() method. (The first snippet posted above also uses the content type framework).
Third, you probably don't want to use multiple inheritance in your model class. It shouldn't be needed and I wouldn't be surprised if there were some obscure side affects. Just have interface.ItemContainer inherit from models.Model and then Order inherit from only interface.ItemContainer.
You can set the related_name argument of a ForeignKey, so if you want to make minimal changes to your design, you could just have ShoppingCartItem and OrderItem set the same related_name on their ForeignKeys to ShoppingCart and Order, respectively (something like "item_set"):
order = models.ForeignKey(Order, related_name='item_set')
and
cart = models.ForeignKey(ShoppingCart, related_name='item_set')