Django - Does the default Django User model have any unique fields? - django

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.

Related

How to apply multiple field options to one field in Django

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.

Django - "ManyToMany" without primary key (AutoField)

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)).

How to deal with many2many using the Django REST Framework

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

Most efficient way to retrieve user with userprofile in Django

In Django, on a recommended setup, a UserProfile instance is linked by a OneToOneField with its User instance.
class UserProfile(models.Model):
user = models.OneToOneField(User)
data = ...
What is the most efficient way inside a view for retrieving both, user and profile?
Can I do a select_related() inner join query, to get both objects with one database hit? Or does it always come down to two separate calls? Possibly, Django's auth middleware retrieves the user instance even before the view is called ... does anybody know?
The user profile can be retrieved using get_profile().
See documentation: https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users

django reverse foreignkey lookup

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.