-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Uzupełnienie scenariusza Django: modele i panel administracyjny.
- Loading branch information
Showing
4 changed files
with
178 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,7 @@ | ||
# -*- coding: utf-8 -*- | ||
from django.contrib import admin | ||
|
||
# Register your models here. | ||
from .models import Pizza, Skladnik | ||
from django.forms import Textarea | ||
from django.db import models | ||
|
||
|
||
class SkladnikInline(admin.TabularInline): | ||
model = Skladnik | ||
max_num = 6 | ||
extra = 3 | ||
fields = ['nazwa', 'jarski'] | ||
|
||
|
||
class PizzaAdmin(admin.ModelAdmin): | ||
# fields = ['przedmiot', 'kategoria', 'typ', 'polecenie'] | ||
exclude = ('autor',) | ||
# obiekty zależne do wyświetlenia | ||
inlines = [SkladnikInline] | ||
# nagłówki do wyświetlenia | ||
# list_display = ('question_text', 'pub_date', 'was_published_recently') | ||
# wyszukiwanie | ||
search_fields = ['nazwa'] | ||
list_per_page = 10 | ||
|
||
formfield_overrides = { | ||
models.TextField: {'widget': Textarea(attrs={'rows': 2, 'cols': 100})}, | ||
} | ||
|
||
def save_model(self, request, obj, form, change): | ||
if not change: | ||
obj.autor = request.user | ||
obj.save() | ||
|
||
|
||
# rejestrujemy model Pizza w panelu administracyjnym | ||
admin.site.register(Pizza, PizzaAdmin) | ||
# rejestrujemy modele w panelu administracyjnym | ||
admin.site.register(Pizza) | ||
admin.site.register(Skladnik) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import models | ||
|
||
|
||
class Pizza(models.Model): | ||
LARGE = 'L' | ||
MEDIUM = 'M' | ||
SMALL = 'S' | ||
ROZMIARY = ( | ||
(LARGE, 'duża'), | ||
(MEDIUM, 'średnia'), | ||
(SMALL, 'mała'), | ||
) | ||
nazwa = models.CharField(verbose_name='Pizza', max_length=30) | ||
opis = models.TextField(blank=True, help_text='Opis Pizzy') | ||
rozmiar = models.CharField(max_length=1, choices=ROZMIARY, default=LARGE) | ||
cena = models.DecimalField(max_digits=5, decimal_places=2) | ||
data = models.DateField('dodano', auto_now_add=True) | ||
|
||
|
||
class Skladnik(models.Model): | ||
pizza = models.ForeignKey(Pizza, related_name='skladniki') | ||
nazwa = models.CharField(verbose_name=u"składnik", max_length=30) | ||
jarski = models.BooleanField( | ||
default=False, | ||
verbose_name=u"jarski?", | ||
help_text=u"Zaznacz, jeżeli składnik jest odpowiedni dla" | ||
u" wegetarian") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import models | ||
from django.contrib.auth.models import User | ||
|
||
|
||
class Pizza(models.Model): | ||
LARGE = 'L' | ||
MEDIUM = 'M' | ||
SMALL = 'S' | ||
ROZMIARY = ( | ||
(LARGE, 'duża'), | ||
(MEDIUM, 'średnia'), | ||
(SMALL, 'mała'), | ||
) | ||
nazwa = models.CharField(verbose_name='Pizza', max_length=30) | ||
opis = models.TextField(blank=True, help_text='Opis Pizzy') | ||
rozmiar = models.CharField(max_length=1, choices=ROZMIARY, default=LARGE) | ||
cena = models.DecimalField(max_digits=5, decimal_places=2) | ||
data = models.DateField('dodano', auto_now_add=True) | ||
autor = models.ForeignKey(User) | ||
|
||
def __unicode__(self): | ||
return u'%s' % (self.nazwa) | ||
|
||
class Meta: | ||
verbose_name_plural = 'pizze' | ||
|
||
|
||
class Skladnik(models.Model): | ||
pizza = models.ForeignKey('Pizza', related_name='skladniki') | ||
nazwa = models.CharField(verbose_name=u"składnik", max_length=30) | ||
jarski = models.BooleanField( | ||
default=False, | ||
verbose_name=u"jarski?", | ||
help_text=u"Zaznacz, jeżeli składnik jest odpowiedni dla" | ||
u" wegetarian") | ||
|
||
def is_jarski(self): | ||
return self.jarski | ||
|
||
def __unicode__(self): | ||
return u'%s' % (self.nazwa) | ||
|
||
class Meta: | ||
verbose_name = "składnik" | ||
verbose_name_plural = "składniki" |