I want to have django user profile where the user can add some objects (he can add more than one) e.g. his addresses, his products and their descriptions and so on. I've no idea how to do that.
The best way to do this is to extend the user model via AUTH_PROFILE_MODULE. There's a great tutorial (doing part of what you want) on James Bennett's blog.
Create a new model (say UserProfile) with a ForeignKey to the User model.
class UserProfile(models.Model):
address = models.TextField()
products = models.TextField()
user = models.ForeignKey(User, unique=True)
Related
So I have two models.
class Post(models.Model):
id = models.OidField('object id', unique=True)
class ArchivedFlag(models.Model):
post = models.ForeignKey(post,
on_delete=models.CASCADE,
related_name='archived_flag')
user = models.ForeignKey(User,
on_delete=models.CASCADE,
related_name='archives')
In views.py I have generated a list of all posts by some criteria, 'plist'.
I want to filter the list based on posts that DO NOT have an ArchivedFlag object relationship. Basically the ArchivedFlag model is a tool hide certain posts.
How can I do this? I'm trying to do something along the lines of
plist = plist.exclude(models.ForeignKey.Post exists)
but I'm unsure of the exact syntax.
You can exclude Post objects for which anArchivedFlag exists with:
Post.objects.exclude(archived_flag__isnull=False)
or easier with a simple filter:
Post.objects.filter(archived_flag=None)
Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.
class Building(models.Model):
address = models.CharField(max_length=20, primary_key=True)
employers = models.ManyToManyField(
settings.AUTH_USER_MODEL, related_name="employers",
blank=True)
Suppose n users of model type User and m buildings of model type Building (m << n). I would like in the Admin page to be able to put users into building in the unique way:
a user can be in maximum one building.
a building can have many employers. It can be empty too.
in the Admin page, in the Employers selection widget, in the UPDATE
mode, exclude users that belong to another building. In the CREATE
mode, show only users without a building.
Stack: Django, MySQL.
So, basically you need inside User model one field with Foreign key relationship with Building and you can query it with related name.
example:
class User(AbstractUser):
"""
Your user model
"""
building = models.ForeignKey(Building, related_name='building_employers', on_delete...)
...
Later you can query employers with building_object.building_employers.all()
For question number 3, please check:
https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#django.contrib.admin.TabularInline
I have a normal Django model. I want to give a certain user the ability to add custom fields. I want the model to update during runtime and the database migrations to apply and then the user can do regular CRUD operations.
Example of a simple Model is
class location(models.Model):
AuotLocationID = models.CharField(max_length=200)
LocationID = models.CharField(max_length=200)
Name = models.CharField(max_length=200)
The user should be able to add other model fields such as Postal Code, Population, etc. Also each instance of the model should be linked to each user. For example Bob may want to track certain fields for his location and John may want some other fields.
Thank you for your help
I have an account model in django which has a foreignkey to Payment and a onetoone to Address.
In the Account section in the admin, I can edit a specific model and edit the fields payment and address via a select widget. However how can I filter the options so that it only shows related models. (ie not every address or payment from every user, only the ones from that user).
The RelatedOnlyFieldListFilter seems to only apply to the model list view. Is there a way to use this in the model edit view?
What you are probably looking for is call inlines. It allows you to edit related object directly in the parent model form which in this case would be the Account model. Here is an example of implementation:
class Account(models.Model):
name = models.CharField(max_length=100)
class Payment(models.Model):
account= models.ForeignKey(Account, on_delete=models.CASCADE)
amount= models.CharField(max_length=100)
class Adress(models.Model):
account= models.OneToOneField(Account, on_delete=models.CASCADE)
adress= models.CharField(max_length=100):
class AccountAdmin(admin.ModelAdmin):
inlines = [
Paymentinline,
Adressinline,
]
Note that this is not a complete implementation (you will need to construct both inlines), follow the documentation on inlines for further information but above are the basics to it.
In Django model I am making a table 'followers', which has:
user's id. (this is followed by)
user's id (this is follower)
that's simple a user can follow other users.
How should I define the model in Django?
I tried this, but does not work:
user = models.ForeignKey('self')
follower_id = models.ForeignKey('self')
How should this be done?
thanks
The 'self' argument won't work unless you have a model called self.
Assuming that your assignment model is called Following, and you're using the built in User model then you can do:
class Following(models.Model):
target = models.ForeignKey('User', related_name='followers')
follower = models.ForeignKey('User', related_name='targets')
This will likely need some further uniqueness and validation logic.
Note the related_name attribute, see https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.ForeignKey.related_name. This means that for a given user object you can do user.targets.all() to get users they follow, and user.followers.all() to get users who follow them.
Note also that Django returns target model instances, not IDs, in the ORM. This means that even though the underlying table may be called follower_id, in the python code following.follower will return an actual User object.
Seeing as Following is actually the through table for the many-to-many relationship between Users. I would create a Profile model which extends the Django User model, and then declare the many-to-many relationship (using ManyToManyField).
from django.contrib.auth.models import User
from django.db import models
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
following = models.ManyToManyField(User, related_name='followers')
Use the many to many field.
followers = models.ManyToManyField('self', symmetrical=False)