I'm using Django, is there a way to apply foreign key and ChartField to one field at the same time? Sometimes I want to allow the user to enter a value that is not in the foreign key. I've googled for a long time and found various ways, but I can't find a solution. Please help.
[models.py]
class Supporting(models.Model):
assignment = models.ForeignKey(Assignment, on_delete=models.CASCADE, blank=False, null=True)
Foreign key only allows ids or objects of models that it has relation with. it can never ever accept text. if you want to this field to be a foreign key for other models too then there is a solution. you can use generic foreign key
You Might be looking for a through Model between Supporting and Assignment.
As per your question, you might require multiple fields defining the nature of relation between two models and Through Models are exactly made for that!
Django Through Models Docs
You can't add ForeignKey and text in Django to a single field. You have to create a foreign key (Assignment) object, and then, after a page refresh, you can attach it to the current model.
I the page refresh is not needed, AJAX has to be used.
Related
I know I should be able to find this in the Django documentation but I was having a difficult time. In my application I need to query the default Django User model but I need to do it in a way that it will only ever return one result. For this to happen one of the fields need to be unique.
Does the Django User Model have any unique fields I can use?
Thanks.
Of course it does: username must be unique, otherwise there would be no way of logging in a specific user.
And, as agconti points out, all models have a pk field which must be unique by deifnition.
I am new to Django and I am wondering about how to work with Django when it comes to an existing database
For instance, let say we have the following tables
Table Student
ID primary key
First_Name Text
Last_Name Text
Table Classes
CID primary key
SKF ForeignKey
Class Text
Ok, this database is already created and has data inside and the foreign and primary keys are already set up. Now, when creating our model, how do we tell Django the foreignkey is the SKF field? If you need any more information please let me know. Thank you for your time.
Django has a built-in function (inspectdb) for auto-generating models from an existing database.
Of course it also takes care of generating the corresponding ForeignKeys.
Using the Django REST Framework.
I have a many2many field in one of my models i.e.
attribute_answers = models.ManyToManyField(AttributeAnswers)
This creates its only table hence I don't have mapped in my models.
However, I want the API to allow a POST to this table, but it does not exist in models so in view and Serialize.py I cannot reference it. What is the recommendation for this?
This creates it's only table hence I don't have mapped in my models.
I assume you're talking about the reverse relationship, as attribute_answers will be accessible from the model instance that it's defined on, and will create an appropriate default serializer field.
For the reverse relationship, make sure you set the related_name on the model field, so that you can access the reverse relationship from the AttributeAnswers model.
See here: http://django-rest-framework.org/api-guide/relations.html#reverse-relations
I am a Django dev and I’m stuck with a problem of reverse foreignkey lookup. The problem is described as follows:
I am working on query optimization. I have a model MicroMessage which has a foreignkey to User (from django.contrib.auth.models) as author. Also there are some other classes which have also foreignkey to User (e.g UserProfile).
I need a query which will fetch author of MicroMessage as well as all users related to any other model from which I can access the UserProfile info of that author without any excessive queries. I tried this:
MicroMessage.objects.select_reverse({'authors':'author_set'})
Please assist me. Thanks in advance.
You're looking for select_related
https://docs.djangoproject.com/en/dev/ref/models/querysets/#select-related
messages = MicroMessage.objects.select_related('author', 'author__userprofile')
The second field (a user's profile) is a reverse foreign key but if implemented as a OneToOneField as the docs suggest, can be queried in the manner shown above if the class was named UserProfile.
I've created a referral system on my website. Admittedly, it's late and this might not be the right sort of relationship.
class Referral(models.Model):
referred=models.ForeignKey(User, related_name="referred")
referrer=models.ForeignKey(User,related_name="referrer")
def __unicode__(self):
return self.user.first_name
What would others recommend to represent this relationship? If this is about right, is there a way for me to turn this into a pseudo inline for the User Admin? It's not allowed as currently constructed because there are two foreign keys to teh same table.
Thanks
Actually, this is a ManyToMany relationship between User and itself - the Referral class itself does not add anything to the relationship.
Normally, you can just add the ManyToManyField to the model and it will be displayed in the admin - but I'm guessing that the User class you reference is the built-in django.contrib.auth User, which you can't directly modify. So one way would be to define a UserProfile class which has a ForeignKey to User, and add the ManyToMany relationship there.
Edit in response to comment No, that's not quite what I meant. I'm saying you should drop the Referral class completely. A model which is just two FKs and nothing else - whether they're both to the same model or not - is the through table of a M2M relationship. That relationship, in your case, is between User and itself - but as I say, since you don't want to change User, it is between a new UserProfile model and itself.