Is it even possible to create a model (or a many to many relation) in Django without id field (AutoField) in database?
For example, I have models: Task and User. User can have assigned many Tasks and Task can be assigned to many Users.
Generally, Django will create the relationship table with fields like id, user_id, task_id. Can a id field be omitted? The user_id and task_id fields will be marked as unique_together.
No, it is not possible to create a many to many field with just the user_id and task_id fields.
All Django models must have exactly one primary key field. It is not yet possible to use composite primary keys (e.g. (user_id, task_id)).
Related
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.
I have two models - Account, like checking, wallet, credit, etc. and Transaction which includes each purchase and earning. Transaction has a foreign key to Account.
In Transaction, I want users to select the Account of purchase. I considered the field.choices but the choices are permanent.
I want users to select a choice of Account from Account models. How do I do that?
If you are using a django form for creating/updating transaction model objects, then it will default to a selection field with all of your Account model objects as options. Model foreign key fields default in django forms as a ModelChoiceField which will render to a template as <select>.
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