Model:
from django.db import models
class VilleStation(models.Model):
nomVille = models.CharField(max_length=255)
adresse = models.CharField(max_length=255)
cp = models.CharField(max_length=5)
def __str__(self):
return self.nomVille
admin.py :
from django.contrib import admin
from prixcarbu.models import VilleStation
class VilleStationAdmin(admin.ModelAdmin):
list_display = ('nomVille', 'adresse','cp',)
fields = ('nomVille', 'adresse','cp',)
admin.site.register(VilleStation, VilleStationAdmin)
I imported a CSV file using database browser for SQLite. Table contains the data but admin page doesn't show it.
Do you see empty model in admin or no model at all?
If you don't see your model in the admin at all check if you added your app to INSTALLED_APPS in settings.py.
After closing my computer and reopening, the data is now visible in the admin. Probably a cache problem. Everything is fine now. Thank you all.
Related
I am generating video files triggered by POST request and saving them programmatically into the django model below.
How can I look up the uploaded file itself once uploaded to the database in a similar fashion to VideoUpload.objects(get)?
I don't have users only guests. I am using hash-id-field so far however can change this.
Models.py
from django.db import models
from django.conf import settings
import hashlib
from hashid_field import HashidAutoField
class VideoUpload(models.Model):
hashed_video_file_name = HashidAutoField(primary_key=True)
name = models.CharField(max_length=40)
videofile= models.FileField(upload_to='videos/', null=True)
objects = models.Manager()
I am using Django 2.2. I don't know what I'm missing.
models.py
from django.db import models
class Efesto(models.Model):
nombre = models.CharField(max_length=150)
tipo = models.ForeignKey(Color, blank=True, null=True, on_delete=models.CASCADE)
....
def __str__(self):
return self.nombre
admin.py
from django.contrib import admin
from estrategia import models
# Register your models here.
admin.register(models.Efesto)
Do I need anything else?
When I open the admin, I can't see the Efesto model there. The admin.py file is created automatically by the startapp command. The urls include
...
path('admin/', admin.site.urls),
It has being a while since I code django, and this used to be enough to get the models registered. The app is included in settings.INSTALLED_APPS correctly. Any advice will help.
You have to use admin.site.register(models.ModelName) in order to show the model in django admin.
You can find more about this in their official documentation
https://docs.djangoproject.com/en/2.2/ref/contrib/admin/
I'm using django-simple-history==1.9.0 package with django 1.8.
When I create an object outside the admin and then look at the history of the object in the admin page, it shows a message
This object doesn't have a change history. It probably wasn't added
via this admin site.
I tried setting the user for that object:
user = User.objects.get(username='john')
Poll.objects.get(id=536).history.update(history_user=user)
but that did not solve the problem.
Poll.objects.get(id=536).history.all().count()
returns 1 so there is a history generated.
Any ideas how to make it show the history or how to create an additional history?
I also tried update_change_reason but that did not work at all.
Assuming your django-simple is correctly configured, follow the procedures below
In the model.py file of the app you want to change import django-simple-history, the following excerpt for import:
from simple_history.models import HistoricalRecords
In the model.py file, add the historical attribute as follows:
history = HistoricalRecords()
Example:
from django.db import models
from simple_history.models import HistoricalRecords
class Poll(models.Model):
question = models.CharField(max_length=200)
history = HistoricalRecords()
In order for your changes made outside of admin to appear in Django admin, simply add the following code in the admin.py file:
Import:
from simple_history.admin import SimpleHistoryAdmin
Use the register to configure admin history:
admin.site.register(Pool, SimpleHistoryAdmin)
Example:
from django.contrib import admin
from simple_history.admin import SimpleHistoryAdmin
from .models import Pool
# Register your models here.
admin.site.register(Tag, SimpleHistoryAdmin)
After this your history will appear in the admin.
Sources:
https://django-simple-history.readthedocs.io/en/latest/admin.html
https://django-simple-history.readthedocs.io/en/latest/user_tracking.html
Regards,
Felipe Dominguesche
Web Developer
Apparently I need to create the log in the LogEntry as in the example below because django-simple-history does not track changes outside the admin page:
from django.contrib.admin.models import LogEntry
from django.contrib.admin.models import LogEntryManager, ADDITION, CHANGE
user_id = User.objects.all()[0].id
content_type_id = ContentType.objects.get(model='color').id
object_id = 4
object_repr = 'Color object'
action_flag = CHANGE
change_message = 'you changed it!'
LogEntry.objects.log_action(user_id, content_type_id, object_id, object_repr, action_flag, change_message=change_message)
This is my admin.py. I have created the file by myself and don't know if I need to register it somewhere in settings.
from django.contrib import admin
from .models import Athlete
admin.site.register(Athlete)
This is my models.py:
from django.db import models
class Athlete(models.Model):
firstName = models.CharField(max_length=30)
lastName = models.CharField(max_length=30)
Both files are in my project folder. I don't have any apps. When I go to url/admin/ I expect to be able to create and edit athletes, but I can only edit groups and users.
What more do I need to do to make Athletes editable in admin?
Add your module into settings INSTALLED_APPS list. Probably you forgot it (as you guess in your comment).
I'm working with an existing django project that uses south. Within each app there's a models folder where models are stored in different files. I have added a new file (shown below) but when I attempt to create migration files for the model, South fails to detect the new file and says: "Nothing seems to have changed." My question is what is the correct way to get south to detect this new model? Thanks.
from django.contrib.auth.models import User, Group
from django.db import models
from django.contrib import admin
class AdgroupEmailRecipients(models.model):
users = models.ForeignKey(User)
class Meta:
app_label = 'wifipromo'
class AdgroupEmailRecipientsAdmin(admin.ModelAdmin):
list_display = ('user_first_name', 'user_last_name', 'user_email')
def user_first_name(self, obj):
return obj.users.first_name
user_first_name.short_description = "First Name"
def user_last_name(self, obj):
return obj.users.last_name
user_last_name.short_description = "Last Name"
def user_email(self, obj):
return obj.users.email
user_email.short_description = "Email"
In the __init__.py file of the models folder, you have to import the model for South or even syncdb to detect it. Basically django is just looking for one file with all your models... and if you import it all in init.py that's what the system will see.