Is there a way to save my user registration in another table from the database?
I don't want to mix AdminUsers with CommonUsers (All the tutorials I've seen, they save the registration inside auth_user)
The reasons:
CommonUsers have different columns and informations
CommonUsers can upload content
AdminUsers verify the content uploaded by the CommonUsers and make sure there is nothing inappropriate
What do I need to use? models? modelform? forms?
The best solution for you is to subclass the User model and add the extra columns you need for your CommonUsers. Your code will probably end up looking something like this:
models.py
from django.contrib.auth import models
from django.db.models import *
class CommonUser(models.User):
custom_field1 = CharField(max_length=255, null=True)
custom_field2 = CharField(max_length=255, null=True)
Link to docs
Related
I'm currently building a Django website where staff has control over users, and, within those powers, I'd like to add one where the staff members can add private comments on users so that they can be read by whoever has the power to do so.
So I started building the user models.py here what I did:
class user(models.Model):
name = models.CharField(max_length=30)
comments = models.TextField()
date = models.DateField(default=datetime.now())
def __str__(self):
return self.name
My question: how can I add a comment field every time a staff member wants to? Like, with the above code, I can only have one comment per user.
Everything is appreciated.
Thanks!
There are a few ways to do this.
1. ForeignKey
Make a separate model for the comments and have a ForeignKey to the User model. That way, multiple comments can be linked to the same user.
2. ArrayField
If you're using Postgres database, you can use the ArrayField.
Cons: Editing in the admin panel is not very user friendly.
3. JSONField
You can also keep multiple comments in a JSON array.
Cons: Editing in the admin panel is not user friendly.
P.S.: If you decide to use either ArrayField or JSONField, check out an app I've made called django-jsonform. This will make editing JSON and ArrayField nice and user-friendly.
Is there anyway where we can build logic Using django rest framework
where user can add blog with multiple images and content accordingly and when saved
and retrieved it should be able to display the same kind of UI depening up on the frontend app
same like medium platform
Note:
My question isn't about adding multiple images and content using Rest framework
but its about fetching and displaying the data based on how user sent it the server
For eg:
<Image>
content for that image
<Image2>
content for this image
i just want to know how to associate those images to that content
i want to add content to that image
or is there anyway where we can store image and all the content exacty and save it in TextField
I've searched a lot about this but unfortunately I've not found a way to make this happen
Read about relationships in Django (and SQL in general)
django relations
it sounds like you're looking for something like the following:
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
# Always override the user model provided by Django when starting a project. the docs themselves state that.
pass
class Image(models.Model):
image = models.ImageField()
added = models.DateTimeField(auto_now_add=True)
# using get_user_model to get the User model, always better then referencing User directly
user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, related_name="user_images",
null=False,
blank=False
)
class ImageContent(models.Model):
title = models.CharField(max_length=140, null=False, blank=False)
content = models.TextField(max_length=500)
image = models.OneToOneField(Image, on_delete=models.CASCADE, null=False, blank=False)
Some notes:
I haven't dealt myself with Images field But I remember it does require a special library (pillow).
getting the data in a certain order should be easy enough if you understand the queryset object:
queryset link
using stuff like order_by will help you return the response in the order you like.
the models I've written here are not the only way to achieve the goal you've set, I strongly recommend reading about relations and models in Django.
I've got multiple instances of a model, and each instance has a related email address. However, several instances have the same connected email address but when I put filter['email'] into my admin.py, I get a long list of the instances' emails, i.e. multiple copies of the same email in several cases.
Is there a way I can remove emails being listed multiple times? Or a way of customising the filter view into something a little nicer? (drop down menu maybe?)
I don't have a ManyToManyField relationship currently, or anything like that. I just have instances in my database with the fields name and email. My models.py looks like this:
import ldapdb.models
from ldapdb.models.fields import CharField, IntegerField, ListField
class Item(ldapdb.models.Model):
item = CharField(db_column='item', max_length=30, primary_key=True, unique=True)
email = CharField(db_column='mail', max_length=20)
My admin.py looks like so:
from items.models import Item
from django.contrib import admin
class ItemAdmin(admin.ModelAdmin):
readonly_fields = ('email',)
list_display = ('item', 'email')
list_filter = ['email']
search_fields = ['item']
admin.site.register(Item, ItemAdmin)
Obviously I've been looking at https://docs.djangoproject.com/en/1.3/ref/contrib/admin/ but I can't really see much by the way of customising my admin's filter view.
Can you post some of your code? I'm not entirely sure I understood the relationship between the instances to your email - is it an email field? a ForeighKey to a different model? how is there more than one if it's not a ManyToMany or similar relationship? And how is the filtering done in the admin?
EDIT
Ok now I understand the problem. What you want is not possible. See for the django admin site the fact that they are the same email doesn't matter because it's still a different object. There's no way around that without either specifying that field to be unique or messing with the admin site code.
A better solution would be to configure the email as searchable in the admin model and then when you search for email example#example.com it would bring all matches back.
Another good solution is to make email a different model and link it to the Item model through a ManyToMany relationship. Then you create an EmailAdmin with a method that shows you all related items for each email.
It all depends on what you actually need. Ultimately you might want to write your own view or mess around with the admin site to modify it to what you need.
Also, you might want to change the email from CharField to EmailField. Hope this helps!
I have a model created in Django 1.5 as below:
class Number(models.Model):
phone_number = models.CharField("Phone Number", max_length=10, unique=True)
I set up Django admin as below:
from django.contrib import admin
from demo.models import Message, Number, Relationship, SmsLog
class NumberAdmin(admin.ModelAdmin):
search_fields = ['phone_number']
admin.site.register(Number, NumberAdmin)
I believe Django add "date_created" column to the database automatically (because I know it sorts the data entries by creation time in admin console). Is there a way to view those time/dates in admin console? The closest I have go to is Django tutorial and StackOverflow ,but I do not want to create another column on my own (pub_date in Django official tutorial's example) and add it if possible. Is there a way to do it and if so, could someone show me how to? Thank you!
Django does not automatically add a date_created column. If you want to track the creation date, you have to declare it in your model.
You may be getting the illusion that it does because if you do not specify a sort order in the model or in the admin class for the model, it will default to sorting by primary key, which will increase according to the order the model instances were created.
My model looks like this:
class Asset(models.Model):
serial_number = models.CharField(max_length=100, unique=True)
asset_tag = models.CharField(max_length=100, unique=True)
class WorkOrder(models.Model):
asset = models.ForeignKey(Asset)
Essentially, a work order is submitted and then an admin assigns an asset to the work order. The asset_tag field is a barcode that we can scan in. When editing the work order in the Django admin, by default the asset field is displayed as a <select> widget. What we want to be able to do is have a search field so that we can scan the asset tag and then search for the right asset in the DB to associate with the work order.
I know you can customize the Django admin foreign key to a hard coded query, but I can't figure out how to get it so it does a search based on a field on the admin page.
Did you take a look at raw_id_fields?
It should be pretty to close to what you're after.
If you are using Django >= 2.0, you can take advantage of a feature called autocomplete_fields. You must define search_fields on the related object’s ModelAdmin because the autocomplete search uses it.
Since you have a ForeignKey relationship to Asset in WorkOrder, in the admin.py of your app add the following:
from django.contrib import admin
#admin.register(Asset)
class AssetAdmin(admin.ModelAdmin):
search_fields = ["serial_number", "asset_tag"]
#admin.register(WorkOrder)
class WorkOrderAdmin(admin.ModelAdmin):
autocomplete_fields = ["asset"]
Add the fields you want to use for searching to search_fields, and add define autocomplete_fields as shown in the code above.
Now you can use the autocomplete_fields from django 2.0.
It's quite neat.