I'm trying to use the built in User model provided by Django in my app, but I'm not sure how to implement it (i.e., what does my models.py file actually look like?) I'm starting off by building a simple login form with the Django auth system.
I'm seeing multiple examples on the web where they import User but never define anything. Like so
from django.db import models
from django.contrib.auth.models import User
#nothing similar to defining User (i.e. 'class User: #fields')
What I'm assuming is by importing User, you don't have to define anything, since it's already been defined, but when I go to run "python manage.py sql 'name'" nothing is executed.
You don't need a models file for the user, because Django already supplies one. You just need to ensure that django.contrib.user is in INSTALLED_APPS.
The only reason you would need to import User into another models file is if you wanted to add a ForeignKey from one of your own models to the User model. In all likelihood, you do want to do that eventually; but you don't need to just in order to get the user models created.
Related
I want to use Django only with admin backend without any apps. So actually all I want to do is to use the admin backend to CRUD my database. Now apparently the admin backend does not have a models.py and no views.py.
Do I really need the models.py from an app, or can I easily use only the admin backend to CRUD my database. How would I do this, add a models.py to the admin backend?
First of all, if you want to CRUD something, you will need a model so you can interact with your database (SQLite, Postgres, etc).
However, a model belongs to an app, once this is the core of Django. So, take a look at https://docs.djangoproject.com/en/2.2/topics/db/models/ where you can read more about that.
If you need a tutorial, take a look at https://docs.djangoproject.com/en/2.1/intro/tutorial02/
In summary, yes, you need an app. However, you do not need a view, once there will be no router, I suppose. Just expose your model to the admin, for instance:
from django.contrib import admin
from .models import YourModel
admin.site.register(YourModel)
Hope it helps
I moved an old Django app from 1.4 to 1.8 and everything in the main application ran fine. I then needed to copy everything from server 1 to server 2, and in the process just dumped the mysql data for the admin database and installed to the new database.
Everything came up fine, however when creating a new user I get:
IntegrityError at /admin/auth/user/add/
(1048, "Column 'last_login' cannot be null")
This is a live system with 30 accounts. I found a variety of solutions to this online but I want to make sure it doesn't trash my database.
I only need the admin database fixed up. My business side database and tables are not managed by Django, but is a custom schema (that I inherited).
btw, deleting a user works fine.
This is described in the release notes, along with the steps to migrate your database to allow null values:
The AbstractUser.last_login field now allows null values. Previously, it defaulted to the time when the user was created which was misleading if the user never logged in. If you are using the default user (django.contrib.auth.models.User), run the database migration included in contrib.auth.
If you are using a custom user model that inherits from AbstractUser, you’ll need to run makemigrations and generate a migration for your app that contains that model. Also, if wish to set last_login to NULL for users who haven’t logged in, you can run this query:
from django.db import models
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractBaseUser
UserModel = get_user_model()
if issubclass(UserModel, AbstractBaseUser):
UserModel._default_manager.filter(
last_login=models.F('date_joined')
).update(last_login=None)
Your error suggests that you haven't run the migration that will allow null values for that column.
I'm working on an app which uses two tables from different databases.I manage to make the connection and make the tables structures in models.py, but now one I change the models.py file, I copy one of the tables in another python script, and I put the file elsewhere for other people to use it.My question it is possible in Django to import a model from outside the project? or the package?
The App is called banner_manager and in views.py I want to import a model called user from another project called django_models
when I try to import like this:
from ....models_django import models.py(in models.py it's the class "user" defined) it says: ValueError: Attempted relative import beyond top-level package
You can add this directory to PYTHONPATH for example:
export PYTHONPATH=$PYTHONPATH:/var/python/your-libs
And then just import package as normal:
import models_django
I just started a Django project (there are no apps in it). I activated the admin in settings file and can access the Django administration page. There is a column in Django page to add users; while adding users I get only three fields under personnal info, but I need to store some more information about users. I Googled around and found that I can use user profiles to accomplish this. I tried, but I am having problems.
My aim is to add three more fields to the user table:
role
contact number
other
I need details like: which function I need to write and where to do this.
I found this, but I do not know where I need to write these steps. I would greatly appreciate a more clear explanation of this.
Django User Profiles is what you need. The blog you linked to has clear steps on how to do it. You can check out the Django documentation. http://www.turnkeylinux.org/blog/django-profile also provides a good explanation.
Basically you need to create a new model with User as ForeignKey and define the model in the settings.py as AUTH_PROFILE_MODULE = "django_app.your_profile_modelname". Create the profile and save it just like any other model, and access it using user.get_profile()
Adding a couple of things in response to your questions below:
First, do not create apps as a directory. Use startapp <appname> [destination] as described here. That will create the app directory.
Second, you have to add the app to INSTALLED_APPS in the project's settings file, do a syncdb. Basically, follow the steps in Django tutorial on writing your first app.
Third, UserProfile is a separate model. It is not an extension of User. It is associated with the User just because you added User as the ForeignKey.
Fourth, to be able to see the user profile model in admin, you do exactly what you would do to add any other model to admin page. Create a file names admin.py under your app with:
from django.contrib import admin
from myproject.app.models import UserProfile
admin.site.register(UserProfile)
There are three key concepts to understand:
There is no built in "profile" system in Django, beyond the limited auth app which is really geared just to user login. You are expected to roll your own.
There is nothing magical about a profile record in itslef, it is just like any other record that takes User as a foreign key (or, more properly, a one-to-one field as per the docs). You create it by creating a custom django app (traditionally called profiles) and a model for that app (traditionally called UserProfile, since Profile is not allowed as a model name).
The only thing that sets UserProfile aparts as a model is that you specify it as the AUTH_PROFILE_MODULE which means that it is accessible when called .get_profile() on a User record. That's it. If you set up the UserProfile like so:
def UserProfile(models.Model):
user = models.OneToOneField(User, related_name='profile')
other fields
then you can also access the profile as user.profile rather than user.get_profile() which some people prefer.
Again, nothing magical about the profile model -- it is just a model record like any other model record.
If you want to be able to edit additional fields within the user form that's more complicated; easiest way is probable unregister User and then register it again using your custom ModelAdmin and form class but judging by your question you're probably not at that level yet.
I am able to run a Django Admin site and when I login as the SuperUser I'm able to modify my model as well. This part works perfectly.
When I login as a user that is NOT a super user, the user does not see an option to modify this Model. When I logged in as a Super User and tried to give this user/group the permission to modify this model from the permission list, I couldn't find this permission in the list.
I see the following permissions:
admin|log entry|
.
.
auth|group|...
auth|message|...
auth|permission|...
content types|...
sessions|...
but nothing related to my ModelAdmin. My admin.py looks as follows:
from django.contrib import admin
from pl.models import *
class MyModelAdmin(admin.ModelAdmin):
pass
admin.site.register(MyModel, MyModelAdmin)
Can only a super-user edit models specified via ModelAdmin? What if I want to permission a group to be able to do so for select models. Any way to do this?
Thanks.
You should be able to do that. Try running python manage.py syncdb again.
If you've already run syncdb and the permissions are still not there, then you must not have your app listed in INSTALLED_APPS. Otherwise, Django would automatically create them.
Additionally, you mention "nothing related to my ModelAdmin". The permissions are created for models, not ModelAdmins. You may have just mistyped that, but if you're looking for something related to the ModelAdmin itself, that might be your problem.