How to duplicate a record with all dependencies? - django

In my Django app I have a model and there is another model that refers as Foreign key to my model.
For example:
Order - the parent model
Items - as a child in that order.
What is the easy way to implement functionality for end users to duplicate my order in a way that also all children will get duplicated as well?

If I understand you correctly - you want to make clone of model instance. Then you can read related documentation section. Also there are a couple of answers on SO related: first, second, third

Related

Django Many to Many - polymorphic

In a Django application, I have three models:
Service
Product
AppliedTax
I want to create a many-to-many relationship between Service and AppliedTax and between Product and AppliedTax. Normally I would have an intermediate model (i.e. AppliedTaxTaxable) with three fields (applied_tax_id, taxable_type and taxable_id) and taxable_type + taxable_id would be a composite foreign key for each related model with taxable_type having as value the name of the related model (Service or Product in my case) and the taxable_id field the id of the related record of the respective model.
I understand how to make it work with raw queries, but Is there a "Django" way to do that with a ManyToMany relationship? So far I had no luck with that. I don't have much experience with Django, but I hope there is a way to do that without using raw queries.
Help :)
Well, after some thought, I did a better search and stumbled upon django-polymorphic. Here is a pretty straightforward explanation on how it works, for a basic set up and it does what I am describing in my question. The implemented schema differs a bit from what my description, but in the end we will home one intermediate table for all associated models.

fields automatically added by django

By default, Django automatically gives each model an id field.
Are there any additional fields django's ORM adds automatically? Perhaps in specific cases?
There are only 2 other situations I can think of where fields are automatically created. One is when sub-classing another model. The sub-class will inherit the parent's fields, see here. The other is a Many-to-Many relationship. For a M2M relationship not only will a field get created but an entire intermediate table. Again, the relevant docs
Also, you can avoid having Django create the id field if you specify primary=True for the field you want to use as the primary key. See here
There are some other model/DB naming conventions as well. For example, the actual database table names will be prefixed with the Django app name that contains them plus an underscore. For example, a model named Author in an app named library will get called library_author. I'm sure there are other examples as well, so this is not an exhaustive list.

Third-party-app for merging Django user objects?

I need to merge two users in a Django database.
So I wonder if there is any simple way (maybe a dedicated app) to do that?
For example:
We have user_a and user_b and some models that have foreign keys to the User model (Books, Interests, Teams and so on…).
By merging users, I want to delete the object user_b and to set all foreign keys pointing to this object to point to user_a. And – this is my main concern – I want the objects that need to be changed because they reference the to-be-deleted object to be determined automatically without having to specify a list of those Models and foreign key fields in them manually.
Is this already implemented and I'm reinventing the wheel?
Is this possible?
If not, please show me the way to do it: how can I build a list of Django models that have a foreign key to a specific model (User in my case) in runtime?
Thank you for your time.
I found this snippet http://djangosnippets.org/snippets/2283/ . I'm going to have to modify it though, to make it recursive. I will share my code once I'm done.
With Django 1.8 and beyond, you could achieve this robustly using the Model _meta API.
Specifically, you could use Options.get_fields. This will even let you handle generic relations.
You'll need to consider for each related field whether you want to add or replace on merge. This decision depends on your application logic and corresponding schema choices.
u = User.objects.get(pk=123)
related_fields = [
f for f in u._meta.get_fields()
if (f.one_to_many or f.one_to_one)
and not f.concrete
]
for f in related_fields:
# use field's attributes to perform an update

Update django foreign key field

I have a foreign key field called books (in the model Book) in an intermediate model called Link_Book_Courses.
I'd like to add multiple Book objects to this. How do I do that in django?
Related Objects have an add() method:
https://docs.djangoproject.com/en/1.3/ref/models/relations/#django.db.models.fields.related.RelatedManager.add
You can add several like this
mymodel.related.add(*OtherModel.objects.filter(...))
For one ForeignKey you can only have one book. So to add multiple entries, you’ll have to add multiple Link_Book_Courses-objects. However what you might really want is a ManyToManyField.

How do you make a Django app pluggable?

Say for example I have a Blog app that I want to be able to drop into different projects, but I always want the Blog to be associated with some other model. For example, in one case I may want it to be associated with a user:
site.com/someuser/blog
But on another site I want it to be associated with, say, a school:
site.com/someschool/blog
Is there a way to make the Blog app pluggable so that it's not necessary to redefine the model (adding a foreign key field) whenever I drop it into a project?
There are several important details for making sure an app can be reusable and I think it's best to link to two of the more important sets of documentation on the topic:
django-best-practices
django-reusable-apps-docs
You might want to look into the ContentTypes framework, I used it to create a comment app that can be used for commenting any model in the database (for different reasons, I didn't want to use the standard django comment app).
http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/
Generic relationships allow you to have a foreign key to any other model. However it's not clear from your question what type of object you want a foreign key to link to. I suspect that foreign key relationship isn't really generic - you just haven't spotted another part of your system that could also be a reusable app.