I started a django tutorial "http://www.lightbird.net/dbe/photo.html", and have followed the instructions for 2 of their apps and neither is working. I am trying to register these models in admin and all I see on the admin page is "Authentication and Authorization, Groups, Users" stacked on top of each other. Where are my album, tags, and image admins? I ran syncdb before starting runserver
from django.db import models
from django.contrib import admin
from django.contrib.auth.model import User
class Album(models.Model):
title = models.CharField(max_length=60)
public = models.BooleanField(default=False)
def __unicode__(self):
return self.title
class Tag(models.Model):
tag = models.CharField(max_length=50)
def __unicode__(self):
return self.tag
class Image(models.Model)
title = models.CharField(max_length=60, blank=True, null=True)
image = models.FileField(upload_to="images/")
tags = models.ManyToManyField(Tag, blank=True)
albums = models.ManyToManyField(Album, blank=True)
created = models.DateTimeField(auto_now_add=True)
rating = models.IntegerField(default=50)
width = models.IntegerField(blank=True, null=True)
height = models.IntegerField(blank=True, null=True)
user = models.ForeignKey(User, blank=True, null=True)
def __unicode__(self):
return self.image.name
class AlbumAdmin(admin.ModelAdmin):
search_field = ["title"]
list_display = ["title"]
class TagAdmin(admin.ModelAdmin):
list_display = ["tag"]
class ImageAdmin(admin.ModelAdmin):
search_fields = ["title"]
list_display = ["__unicode__", "title", "user", "rating", "created"]
list_filter = ["tags", "albums"]
admin.site.register(Album, AlbumAdmin)
admin.site.register(Tag, TagAdmin)
admin.site.register(Image, ImageAdmin)
Related
I'm trying to show my all children category from parent category. I want to just hit one API end and show all tables which is related to that item. I want to hit "Master-Category" and show all releated "Category","Sub-Category" and "Root-Item" in Hierarchy form. I display all the data but cannot in Hierarchy form. Can anyone please give me the solution for this problem.
Model.py
from django.db import models
from django.contrib.auth.models import User
class MasterCategory(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True,
verbose_name="Created By")
title = models.CharField(max_length=100, null=False, blank=False)
description = models.TextField(default='')
def __str__(self):
return str(self.title)
#property
def category(self):
data = NewCategory.objects.filter(master_category__id=self.id).values
return data
#property
def sub_category(self):
data = NewSubcategory.objects.filter(category__id=self.id).values
return data
#property
def root_item(self):
data = Rootitem.objects.filter(sub_category__id=self.id).values
return data
class NewCategory(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True,
verbose_name="Created By")
title = models.CharField(max_length=100, null=False, blank=False)
description = models.TextField(default="")
master_category = models.ForeignKey(
MasterCategory, on_delete=models.CASCADE, null=True, blank=True)
def __str__(self):
return str(self.title)
class NewSubcategory(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True,
verbose_name="Created By")
title = models.CharField(max_length=100, null=False, blank=False)
description = models.TextField(default="")
category = models.ForeignKey(NewCategory, on_delete=models.CASCADE, null=True,
blank=True)
def __str__(self):
return str(self.title)
class Rootitem(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True,
verbose_name="Created By")
title = models.CharField(max_length=100, null=False, blank=False)
description = models.TextField(default="")
sub_category = models.ForeignKey(NewSubcategory, on_delete=models.CASCADE,
null=True, blank=True)
def __str__(self):
return str(self.title)
Serializers.py
I add #property function name in MasterCategorySerializer fields, "category", "sub_category", "root_item"
from .models import MasterCategory, NewCategory, NewSubcategory, Rootitem
from rest_framework import serializers
class MasterCategorySerializer(serializers.ModelSerializer):
class Meta:
model = MasterCategory
fields = ["title", 'category', 'sub_category', 'root_item']
class NewCategorySerializer(serializers.ModelSerializer):
class Meta:
model = NewCategory
fields = "__all__"
class NewSubcategorySerializer(serializers.ModelSerializer):
new_cat = NewCategorySerializer(source='category',read_only=True, many=True)
class Meta:
model = NewSubcategory
fields = "__all__"
class RootitemSerializer(serializers.ModelSerializer):
class Meta:
model = Rootitem
fields = "__all__"
**Viewset.py**
from API_app.models import MasterCategory
from API_app.serializers import MasterCategorySerializer
from rest_framework import viewsets
class MasterCategoryViewSet(viewsets.ModelViewSet):
queryset = MasterCategory.objects.all()
serializer_class = MasterCategorySerializer
My Desired Output, what i want.
{
Electronics <---- Master-Category
{
Smart-Phone <---- Category
{
Samsung <---- Sub-Category
{
Samsung S20 Ultra <---- Root-Item
}
}
}
}
Change your serializers as below. For this nested structure you don't need properties. As tables are connected with foreign key you can define related name between models and assign to its serializer. Default related name between table is tablename_set.
class RootitemSerializer(serializers.ModelSerializer):
class Meta:
model = Rootitem
fields = "__all__"
class NewSubcategorySerializer(serializers.ModelSerializer):
rootitem_set = RootitemSerializer(many=True)
class Meta:
model = NewSubcategory
fields = "__all__"
class NewCategorySerializer(serializers.ModelSerializer):
newsubcategory_set = NewSubcategorySerializer(many=True)
class Meta:
model = NewCategory
fields = "__all__"
class MasterCategorySerializer(serializers.ModelSerializer):
newcategory_set = NewCategorySerializer(many=True)
class Meta:
model = MasterCategory
fields = "__all__"
We have relationship between MasterCategory and NewCategory. As you don't define related_name therefore related name is newcategory_set and its response is NewCategorySeralizer. Make many=True because they are related with foreign key as there can be multiple newcategory related to mastercategory. Other relations are same as above explanation.
If you want to change this default related name then look at related_name, you can define it inside models.ForeignKey()
I'm building an import excel files system for every leads whit an import-export library. On the Website, each user must be able to import his leads and make sure that they are viewed only by him. In all other cases, I filtered the "organisation" field linked to a UserProfile model through the views.py. But now I don't know how to filter the field organisation for a specific user. At the moment I can import the excel files from the template but leave the organisation field blank. Help me please I'm desperate
Models.py
class Lead(models.Model):
nome = models.CharField(max_length=20)
cognome = models.CharField(max_length=20)
luogo=models.CharField(max_length=50, blank=True, null=True, choices=region_list)
città =models.CharField(max_length=20)
email = models.EmailField()
phone_number = models.CharField(max_length=20)
description = models.TextField()
agent = models.ForeignKey("Agent", null=True, blank=True, on_delete=models.SET_NULL)
category = models.ForeignKey("Category", related_name="leads", null=True, blank=True, on_delete=models.SET_NULL)
chance=models.ForeignKey("Chance",related_name="chance", null=True, blank=True, on_delete=models.CASCADE)
profile_picture = models.ImageField(null=True, blank=True, upload_to="profile_pictures/")
converted_date = models.DateTimeField(null=True, blank=True)
date_added = models.DateTimeField(auto_now_add=True)
organisation = models.ForeignKey(UserProfile, on_delete=models.CASCADE,null=True, blank=True)
objects = LeadManager()
age = models.IntegerField(default=0)
def __str__(self):
return f"{self.nome} {self.cognome}"
class User(AbstractUser):
is_organisor = models.BooleanField(default=True)
is_agent = models.BooleanField(default=False)
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
def __str__(self):
return self.user.username
Views.py
def simple_upload(request):
if request.method == 'POST':
Lead_resource = LeadResource()
dataset = Dataset()
newdoc = request.FILES['myfile']
imported_data = dataset.load(newdoc.read(),format='xlsx')
#print(imported_data)
for data in imported_data:
value = Lead(
data[0],
data[2],#nome
data[3],#cognome
data[5],#luogo
data[7],#cittÃ
data[8],#email
data[9],#numero telefono
data[11],#desc
)
value.save()
result = Lead_resource.import_data(dataset, dry_run=True) # Test the data import
if not result.has_errors():
Lead_resource.import_data(dataset,dry_run=False) # Actually import now
return render(request, 'input.html')
Resources.py
class LeadResource(resources.ModelResource):
nome = fields.Field(attribute='nome', column_name='nome')
luogo = fields.Field(attribute='luogo', column_name='regione')
class Meta:
model = Lead
report_skipped=True
admin.py
#admin.register(Lead)
class PersonAdmin(ImportExportModelAdmin):
readonly_fields = ('date_added',)
I am having problem with list_display/joining two table for django admin interface.
I want to have columns- program_name and is_active from SchPrograms model with parameter_name, parameter_description from VisVisitParameters model in django admin. I am able to have those columns which i am using with return in each of these models. I tried to take help from the question that already has been asked. But still i could not able to figured this out.
class SchPrograms(models.Model):
program_id = models.AutoField(primary_key=True)
program_name = models.CharField(max_length=200, blank=True, null=True)
creation_timestamp = models.DateTimeField(blank=True, null=True)
is_active = models.IntegerField()
class Meta:
managed = True
db_table = 'sch_programs'
app_label = 'polls'
def __str__(self):
return self.program_name
class VisVisitParameters(models.Model):
vparameter_id = models.AutoField(primary_key=True)
parameter_name = models.CharField(max_length=300, blank=True, null=True)
parameter_description = models.TextField(blank=True, null=True)
is_active = models.IntegerField(choices=STATUS_CHOICES)
class Meta:
managed = False
db_table = 'vis_visit_parameters'
def __str__(self):
return str(self.vparameter_id)
app_label = 'polls'
class VisVisitParameterMappings(models.Model):
vp_map_id = models.AutoField(primary_key=True)
vparameter = models.ForeignKey(VisVisitParameters,on_delete=models.CASCADE)
program = models.ForeignKey(SchPrograms,on_delete=models.CASCADE)
display_order = models.IntegerField(blank=True, null=True)
is_active = models.IntegerField()
class Meta:
managed = False
db_table = 'vis_visit_parameter_mappings'
app_label = 'polls'
def __str__(self):
return str(self.parameter_name)
model.py
class VisVisitParameterMappings_admin(admin.ModelAdmin):
list_display = ('program_name','is_active','parameter_name','parameter_description ')
To display the required items on the list display page you can write your custom methods, as documented here.
For example, for your field named program_name you can have:
def program_name(self, obj):
if obj.program:
return obj.program.program_name
program_name.short_description = 'Program name'
and for another model field named parameter_name , you may have:
def parameter_name(self, obj):
if obj.vparameter:
return obj.vparameter.parameter_name
parameter_name.short_description = 'Parameter Name'
Hope it helps.
I've made a foreign key relationship with django User model, the forward lookup is working fine but when I try to backward is throwing this error:
'QuerySet' object has no attribute 'urlpost_set'
I have also tried the related name! Also note that the Catagory to PostUrl and PostUrl to Catagory is working just fine!
My models.py:
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Catagory(models.Model):
title = models.CharField(max_length=15, unique=True)
created = models.DateField(auto_now_add=True)
updated = models.DateField(auto_now=True)
def __str__(self):
return self.title
class Meta:
verbose_name_plural = 'catagory'
class UrlPost(models.Model):
STATUS_CHOICES = (
('public', 'Public'),
('private', 'Private'),
)
profile = models.ForeignKey(User, related_name='user_post', on_delete=models.CASCADE)
catagory = models.ForeignKey(Catagory, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
slug = models.SlugField(unique=True)
url = models.URLField()
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='public')
note = models.TextField(blank=True)
created = models.DateField(auto_now_add=True)
updated = models.DateField(auto_now=True)
class Meta:
ordering = ['-created']
verbose_name_plural = 'url Post'
def __str__(self):
return self.title
You have set related_name='user_post' while defining ForeignKey relation between your User model and UrlPost.
You have to use .user_post.all() instead of .urlpost_set.all() in your queryset.
How can i make field related to field of related model. I have two models: album and photo, album model have boolean field private. How to create field private in photo, which will have False value if album field equal to None and value of album private field if it's not.
models.py:
from django.db import models
from django.contrib.auth.models import User
import os
import uuid
def get_image_path(instance, filename):
return '{}.{}'.format(uuid.uuid4(), filename.split('.')[-1])
class Album(models.Model):
user = models.ForeignKey(User, related_name='albums', on_delete=models.CASCADE)
title = models.CharField(max_length=80, default='New album')
creation_date = models.DateField(auto_now_add=True)
private = models.BooleanField(default=False)
def __str__(self):
return self.title
class Meta:
ordering = ['-creation_date', ]
class Photo(models.Model):
user = models.ForeignKey(User, related_name='photos', on_delete=models.CASCADE)
album = models.ForeignKey(Album, related_name='photos', on_delete=models.CASCADE, null=True, blank=True)
title = models.CharField(max_length=80, default='New photo')
image = models.ImageField(title, upload_to=get_image_path)
creation_date = models.DateField(auto_now_add=True)
# ???
private = models.BooleanField()
# ???
def __str__(self):
return self.title
class Meta:
ordering = ['-creation_date', ]
As #RodXavier said just use property
class Photo(models.Model):
user = models.ForeignKey(User, related_name='photos', on_delete=models.CASCADE)
album = models.ForeignKey(Album, related_name='photos', on_delete=models.CASCADE, null=True, blank=True)
title = models.CharField(max_length=80, default='New photo')
image = models.ImageField(title, upload_to=get_image_path)
creation_date = models.DateField(auto_now_add=True)
#property
def private(self):
return getattr(self, 'album', False)