I've got the asp.net forms authentication tables in place, and I'd like to create a FK to one of the tables.
Is this possible without creating a type to be used with the attribute?
In the end, I created another skelton poco class that matched the table, this enabled me to place the foreign key.
Related
I need to change some objects primary keys in my django app, I wonder how can I achieve that?
Also my model has relations with other models.
Technically, it should be possible with an update query on the queryset:
MyModel.objects.filter(id=old_id).update(id=new_id)
The relations should cascade too if the constraints in the database have been set up correctly, but in general, I'd avoid updating PKs.
I'm trying to use the oauth2_provider library which provides a model for AccessToken, which foreign keys into a User model. My User model will actually live in a different database from the OAuth2 token models. I can use a router to direct which DB to use for a particular model, but am I correct in concluding that Django would not support a foreign key to a model in a different DB?
If I still wanted to extend from the AbstractAccessToken with my User in a different DB, is there any way that Django allows me to populate the user_id foreign key column at all? Or would I simply need to leave it null and define and have my custom AccessToken class define its own unconstrained external_user_id column?
Django doesn't support any ForeignKey operations that span multiple databases. So, as you suggested, I think the best you can do is to provide your own IntegerField for the user and use it manually. Unfortunately that may require a lot of fiddling with that third-party library if it has a lot of internal code that's expecting to pull the user from the database.
I am using django-salesforce and I would like to create a model within Django that has a ForeignKey field pointing to a SFDC model (hosted on force.com).
I created a custom model on force.com, let us call it SFModel, and I can successfully work on it from django (CRUD) by subclassing salesforce.models.Model.
I also created a django.db.models.Model, let us call it DJModel, that has a unique field ForeignKey(SFModel). This model is registered on the admin panel.
All models validate and I can go to my admin panel to try to create a new instance of DJModel. However, when I try to display the create_form in the admin I get the following error :
hasattr(): attribute name must be string
and the debug stream says
So I tried to set an arbitrary alias to the SF entry in the DATABASES of my settings.py. There is a dedicated variable for that :
SALESFORCE_DB_ALIAS = 'youralias'
But I still have the same problem.
Any recommendation?
Django doesn't support it and an external reference to Salesforce should be currently saved as a CharField and a reference to other databases as IntegerField.
Django docs about Limitations of multiple databases:
Django doesn’t currently provide any support for foreign key or many-to-many relationships spanning multiple databases. If you have used a router to partition models to different databases, any foreign key and many-to-many relationships defined by those models must be internal to a single database.
I tried the cross reference with sqlite as 'default' database. It was possible to create an object of model DJModel with cross-database reference from sqlite to Salesforce. It behaves similarly to normal Django cross-database references, without obscure errors and only a dot reference can be used.
EDIT: Simplified after many years.
I really like the idea of the sites framework for making an application functional across multiple web sites. Is there a way to add fields onto the Site object for the database, or should I just create a foreign-key one-to-one relationship?
Yes, use a model with a unique foreign key. Django will add it to Site as a reverse relationship.
I have three Django models that exist in a hierarchy:
Protocol -> has Tests
Test -> has Cases
The Test model has a foreign key to Protocol, and the Case model has foreign keys to Test and Protocol. A pretty simple structure.
Each of these models has an attribute called owner and any user in my app can own anything. What I want to do is create a query that gives me a unique set of all of the owners of any instance of these three models. I haven't been able to find a nice way to do this in Django. Any help is appreciated.
You can't do it without querying all the models individually.
User.objects.filter(Q(protocol__isnull=False) | Q(test__isnull=False) | Q(case__isnull=False)