serializers.py
class RegSerializer(serializers.ModelSerializer):
admin = serializers.SlugRelatedField(slug_field='username', read_only=True)
class Meta:
model = Registration
fields = [
'id', 'rooms', 'first_name', 'last_name','admin', 'pasport_serial_num', 'birth_date', 'img', 'visit_date',
'leave_date', 'guest_count', 'room_bool']
models.py
class Rooms(models.Model):
objects = None
room_num = models.IntegerField(verbose_name='Комната')
room_bool = models.BooleanField(default=True, verbose_name='Релевантность')
category = models.CharField(max_length=150, verbose_name='Категория')
price = models.IntegerField(verbose_name='Цена (сум)', null=True)
def __str__(self):
return f'{self.room_num}'
class Meta:
verbose_name = 'Комнату'
verbose_name_plural = 'Комнаты'
class Registration(models.Model):
objects = None
rooms = models.ForeignKey(Rooms, on_delete=models.CASCADE, verbose_name='Номер',
help_text='Номер в который хотите заселить гостя!',
)
first_name = models.CharField(max_length=150, verbose_name='Имя')
last_name = models.CharField(max_length=150, verbose_name='Фамилия')
admin = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='Администратор')
pasport_serial_num = models.CharField(max_length=100, verbose_name='Серия паспорта', help_text='*AB-0123456')
birth_date = models.DateField(verbose_name='Дата рождения')
img = models.FileField(verbose_name='Фото документа', help_text='Загружайте файл в формате .pdf')
visit_date = models.DateField(
default=django.utils.timezone.localdate, verbose_name='Дата прибытия')
leave_date = models.DateField(blank=True, null=True, verbose_name='Дата отбытия', default='После ухода!')
guest_count = models.IntegerField(default=1, verbose_name='Кол-во людей')
room_bool = models.BooleanField(default=False, verbose_name='Релевантность',
help_text='При бронирование отключите галочку')
price = models.IntegerField(verbose_name='Цена (сум)', null=True)
def __str__(self):
return f'{self.rooms},{self.last_name},{self.first_name},{self.room_bool}'
class Meta:
verbose_name = 'Номер'
verbose_name_plural = 'Регистрация'
how can I make it so that the name of the user who registered room is indicated in the admin field and without the right to change only readonly?
can this be done at all?
thanks in advance for your reply
You can pass additional attributes to serilizer's save method. In your view, you can call serializer save() with admin argument like this:
def your_view(request):
# your code
serializer.save(admin=request.user)
Or if you want to do it on admin page, you can override your admin's save_model method. Also you should specify admin as a readonly:
class RegistrationAdmin(admin.ModelAdmin):
readonly_fields = ('admin',)
def save_model(self, request, obj, form, change):
if not obj.pk:
# Only set admin during the first save.
obj.admin = request.user
super().save_model(request, obj, form, change)
I set serializers.HiddenField and by default set CurrentUserDefault() from serializers
automatically substitutes the value of the current admin and at the same time the admin field now goes to the api
class RegSerializer(serializers.ModelSerializer):
admin = serializers.HiddenField(default=serializers.CurrentUserDefault())
class Meta:
model = Registration
exclude = ['price', 'visit_date']
Related
I created a ProductAttributes model that have a ForeignKey from Product model
now i'm trying to create an admin panel for adding product using django admin
i'm adding ProductAttributes to Product admin with TabularInline but its not working
this the models and admin classes
#models
class Product(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField()
description = models.TextField(null=True, blank=True)
introduction = models.TextField(null=True, blank=True)
unit_price = models.DecimalField(
max_digits=12,
decimal_places=2,
validators=[MinValueValidator(1)])
inventory = models.IntegerField(validators=[MinValueValidator(0)])
last_update = models.DateTimeField(auto_now=True)
collection = models.ForeignKey(Collection, on_delete=models.PROTECT, related_name='products')
promotions = models.ManyToManyField(Promotion, blank=True)
def __str__(self) -> str:
return self.title
class Meta:
ordering = ['title']
class ProductAttributes(models.Model):
Product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name="attributes")
attribute = models.CharField(max_length=255)
#admin
class ProductAttributesInline(admin.TabularInline):
model = models.ProductAttributes
#admin.register(models.Product)
class ProductAdmin(admin.ModelAdmin):
autocomplete_fields = ['collection']
prepopulated_fields = {
'slug': ['title']
}
actions = ['clear_inventory']
inlines = [ProductAttributesInline]
list_display = ['title', 'unit_price',
'inventory_status', 'collection_title']
list_editable = ['unit_price']
list_filter = ['collection', 'last_update', InventoryFilter]
list_per_page = 10
list_select_related = ['collection']
search_fields = ['title']
def collection_title(self, product):
return product.collection.title
#admin.display(ordering='inventory')
def inventory_status(self, product):
if product.inventory < 10:
return 'Low'
return 'OK'
#admin.action(description='Clear inventory')
def clear_inventory(self, request, queryset):
updated_count = queryset.update(inventory=0)
self.message_user(
request,
f'{updated_count} products were successfully updated.',
messages.ERROR
)
class Media:
css = {
'all': ['store/style.css']
}
the ProductAttributes isnt shown in Product admin
in the orginal project i created another inline for ProductImage and its working but when i try to delete that inline its not gone from product admin
Firstly, do not forget checking all migrations, and It would be more good to keep your models in models.py and do not mix them with admin related changes. I would recommend you to write them in admin.py.
You can use both images and attributes like that:
class ProductAttributesInlineAdmin(admin.TabularInline):
model = ProductAttributes
extra = 2
#admin.register(models.Product)
class ProductAdmin(admin.ModelAdmin):
...
inlines = [ProductAttributesInlineAdmin, ProductImageInlineAdmin]
Each model is chain-linked to the previous model. I make one form to fill in, since all the models are linked. When I try to link a model address to a project, an error appears
NOT NULL constraint failed: crm_project.address_id
/crm/views.py, line 39, in add_project
form_project.save()
Models
class City(models.Model):
obl = models.CharField(max_length=255, choices=REGIONS, default="24", verbose_name="Регион")
name = models.CharField(max_length=128, verbose_name="Город")
population = models.IntegerField()
def __str__(self):
return self.name
class Address(models.Model):
city = models.ForeignKey(City, on_delete=models.PROTECT, verbose_name="Город")
street = models.CharField(max_length=255, verbose_name="Улица")
numb = models.CharField(max_length=64, verbose_name="Номер дома")
def __str__(self):
return f"{self.street}, {self.numb}"
class Project(models.Model):
manager = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name="Сотрудник")
address = models.ForeignKey(Address, on_delete=models.PROTECT, verbose_name="Адрес")
# another fields
class ProjectDetail(models.Model):
project = models.OneToOneField(Project, on_delete=models.CASCADE, verbose_name="Проект")
# another fields
Forms
class AddressForm(forms.ModelForm):
class Meta:
model = Address
fields = ["city", "street", "numb"]
def __init__(self, *args, **kwargs):
city = kwargs.pop("city", "")
super(AddressForm, self).__init__(*args, **kwargs)
self.fields["city"] = forms.ModelChoiceField(queryset=City.objects.all())
class ProjectForm(forms.ModelForm):
class Meta:
model = Project
fields = ["file", "sq", "rent_tax"]
class ProjectDetailForm(forms.ModelForm):
class Meta:
model = ProjectDetail
exclude = ['project', 'comment', 'resume', 'complite', 'created_at']
Views
def add_project(request):
form_address = AddressForm(request.POST or None)
form_project = ProjectForm(request.POST or None)
form_detail = ProjectDetailForm(request.POST or None)
if request.method == 'POST':
if form_address.is_valid() and form_project.is_valid() and form_detail.is_valid():
address = form_address.save(commit=False)
form_project.manager = request.user
form_project.address = address
project = form_project.save(commit=False)
form_detail.project = project
form_address.save()
form_project.save()
form_detail.save()
context = {'form_address':form_address, 'form_project':form_project, 'form_detail':form_detail,}
return render(request, 'crm/project_form.html', context)
in your ProjectForm you specified these fields only fields = ["file", "sq", "rent_tax"]
but as per Your Model:
class Project(models.Model):
manager = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name="Сотрудник")
address = models.ForeignKey(Address, on_delete=models.PROTECT, verbose_name="Адрес")
There two fields are null=FalseMeans every instance of Project must have these Values which you're not providing in the ProjectForm
to handle this error you can do 2 things as per your need.
replace this form with this:
class ProjectForm(forms.ModelForm):
class Meta:
model = Project
fields = '__all__"
or another option which is not how you should use foreign keys.
change your class with this and makemigrations and migrate again:
class Project(models.Model):
manager = models.ForeignKey(User, on_delete=models.PROTECT,null=True, blank=True, verbose_name="Сотрудник")
address = models.ForeignKey(Address, on_delete=models.PROTECT, null=True, blank=True,verbose_name="Адрес")
This is the model that I want to show on the admin panel. I'm registering the model via admin.py file with admin.site.register(Ad). I tried to re-write the register line twice, and an exception appeared that the model is already registered.
class Ad(AdModel):
plate = models.CharField(max_length=50, unique=True)
description = models.TextField(max_length=500)
ad_type = models.CharField(
max_length=255,
choices=AdTypes.get_choices(),
default=AdTypes.OFFERING,
)
price = models.PositiveIntegerField(
default=0,
help_text='In cents'
)
location = models.CharField(
max_length=255,
choices=AdLocations.get_choices(),
default=AdLocations.VILNIUS,
)
user = models.ForeignKey(User, on_delete=models.PROTECT)
approved_date = models.DateField(null=True, blank=True)
approved_by = models.ForeignKey(
User, on_delete=models.PROTECT, related_name='approved_by', null=True
)
The two base models:
class UUIDBaseModel(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
class Meta:
abstract = True
class AdModel(UUIDBaseModel):
expires_at = models.DateTimeField(null=True)
is_draft = models.BooleanField(default=False)
is_active = models.BooleanField(default=False)
class Meta:
abstract = True
This is really strange, maybe that could be the problem because of the naming 'Ad'? I have a serializer for this model and everything works just fine, but the admin panel doesn't want to display it.
views.py
class AdCreateViewSet(ModelViewSet, CreateModelMixin):
serializer_class = AdCreateSerializer
permission_classes = (AllowAny,)
filter_backends = [DjangoFilterBackend]
search_fields = ('plate', 'description', 'user__email')
queryset = Ad.objects.select_related('user')
def perform_create(self, serializer):
user = User.objects.first()
serializer.save(user=user) # self.request.user)
serializers.py
class AdCreateSerializer(CustomAdSerializer):
class Meta:
model = Ad
exclude = ['expires_at']
read_only_fields = ('user',)
To be more detailed, i have got list of objects in admin panel named Images
class Image(models.Model):
image = models.ImageField(upload_to='products/%Y/%m/%d/',
verbose_name=_('Image'), default='default.png')
album = models.ForeignKey('Album', related_name='images')
category = TreeForeignKey(Category, null=True)
likes = GenericRelation('Like', related_name='image_likes', null=True)
is_main = models.BooleanField(default=False)
is_slider = models.BooleanField(default=False)
seen = models.IntegerField(default=0)
seen_by_admin = models.BooleanField(default=False)
class ImageAdmin(admin.ModelAdmin):
list_display = ['album', 'get_owner', 'is_main', 'is_slider','total_likes', 'seen', 'image_tag', ]
list_filter = ['album', 'album__owner', 'is_main', 'is_slider', 'album__created_at']
search_fields = ['album__name']
list_per_page = 15
Whenever admin or any other superuser enters to any Images.object the seen_by_admin field should be changed to seen_by_admin = True
You could override the save_model method:
class ImageAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
obj.seen_by_admin=True
super().save_model(request, obj, form, change)
More info:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model
I'm trying to do a simple commenting app that composed of a title and comments under that title. I can create title and I can see the form for adding comment entries to this title. I type and click submit and it does nothing. When I use admin panel, I can add entries without a problem.
This is my models:
class Baslik(models.Model):
user = models.ForeignKey(User, null=True, blank=True)
title = models.CharField(max_length=50)
slug = models.SlugField()
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
active = models.BooleanField(default=True)
def __unicode__(self):
return str(self.title)
def get_absolute_url(self, ):
return reverse('baslik', args=[self.slug])
class Entry(models.Model):
user = models.ForeignKey(User, null=True, blank=True)
baslik = models.ForeignKey(Baslik, null=True, blank=True)
icerik = models.TextField(max_length=50000)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
def __unicode__(self):
return str(self.icerik)
this is my views' entry part:
def tek(request, slug):
baslik = Baslik.objects.get(slug=slug)
entryler = baslik.entry_set.all()
form2 = EntryForm(request.POST or None)
if form2.is_valid():
entry = form2.save(commit=False)
entry.user = request.user
entry.save()
return HttpResponseRedirect('/baslik/%s'%(baslik.slug))
return render_to_response("baslik/tek.html", locals(), context_instance=RequestContext(request))
this is my forms.py:
class BaslikForm(ModelForm):
class Meta:
model = Baslik
fields = ('title',)
class EntryForm(ModelForm):
class Meta:
model = Entry
fields = ('icerik',)
this is my admin.py:
class EntryInline(admin.TabularInline):
model = Entry
class BaslikAdmin(admin.ModelAdmin):
list_display = ('__unicode__','live_link')
search_fields = ['title']
list_filter = ['timestamp', 'updated']
prepopulated_fields = {"slug": ('title',)}
readonly_fields = ['live_link', 'timestamp', 'updated']
inlines = [EntryInline]
class Meta:
model = Baslik
def live_link(self,obj):
link = "<a href='/baslik/" + str(obj.slug)+ "/'>" + obj.title + "<a/>"
return link
live_link.allow_tags = True
admin.site.register(Baslik, BaslikAdmin)
What should I do to solve this? Please be explanatory. I'm very new at Django.
Thanks.