Python温泉のまとめ
次回も同じところでするようなので、メモ的なものを。
熱海駅から来宮駅に行くには伊豆鉄道を使っていくのですが
なんといっても、本数が少ない。
ということで、とる行動は
- 大富豪的行動をしたい人は、熱海駅で降りタクシーで旅館まで行く。(ただし最後のほうは道案内が必要になってくるかも)
- 富豪的行動は熱海駅からタクシーを乗り来宮駅付近で降り、徒歩で旅館まで。
- 平民的行動は、電車が来るまで待つ。その間に食事など済ませたり構内にあるドトールで待つ。
になると思います。
あと、帰りに気づいたのが旅館の近くにベーカリーみたいなものがあったような気がする。
今度覗いてみよう:)
それと、来宮神社でそば・うどんが食べられるようなので食べてみたい。
今回もid:VoluntasはMercurialの伝道師になっていた :)
newforms-admin(今後のDjango trunk)は、モデル定義とアドミン定義をファイル的に分離することが出来るようになります。
ということで、前回のモデル定義を参考にして分離してみたいと思います。
前回は、
from django.db import models from django.contrib import admin # Create your models here. class Address(models.Model): short_name = models.CharField(max_length=20) long_name = models.CharField(max_length=80) addr_line_1 = models.CharField(max_length=80, blank=True) addr_line_2 = models.CharField(max_length=80, blank=True) city = models.CharField(max_length=50) state = models.USStateField() zip = models.CharField(max_length=10, blank=True) def __str__(self): #return self.short_name return "%s (%s %s)" % (self.short_name, self.city, self.state) class Meta: verbose_name_plural = 'Addresses' class PhoneType(models.Model): description = models.CharField(max_length=20) def __str__(self): return self.description class Phone(models.Model): description = models.CharField(max_length=80) number = models.PhoneNumberField() number_type = models.ForeignKey(PhoneType) address = models.ForeignKey(Address) def __str__(self): return self.number class Date(models.Model): description = models.CharField(max_length=80) start_date = models.DateField() annual_event = models.BooleanField() address = models.ForeignKey(Address) class Note(models.Model): note_text = models.TextField() address = models.ForeignKey(Address) class Group(models.Model): description = models.CharField(max_length=80) addresses = models.ManyToManyField(Address) def __str__(self): return self.description class PhoneInline(admin.TabularInline): model = Phone extra = 3 class DateInline(admin.TabularInline): model = Date extra = 2 class NoteInline(admin.StackedInline): model = Note extra = 1 class GroupOptions(admin.ModelAdmin): filter_horizontal = ('addresses',) class AddressOptions(admin.ModelAdmin): search_fields = ['short_name', 'long_name'] fieldsets = ( ('Name', {'fields': ('short_name', 'long_name')}), ('Address', {'fields': ('addr_line_1', 'addr_line_2', 'city', 'state', 'zip')}), ) list_display = ('short_name', 'city', 'state') ordering = ('short_name',) list_filter = ('state',) inlines = [PhoneInline, DateInline, NoteInline] admin.site.register(Address, AddressOptions) admin.site.register(PhoneType) admin.site.register(Group, GroupOptions)
でした。
アドミン定義の部分を保存するファイルを作成します。
分かりやすいようにadmin.pyとします。
配置場所は、分割するmodels.pyと同じところに置きます。
models.pyは
from django.db import models # Create your models here. class Address(models.Model): short_name = models.CharField(max_length=20) long_name = models.CharField(max_length=80) addr_line_1 = models.CharField(max_length=80, blank=True) addr_line_2 = models.CharField(max_length=80, blank=True) city = models.CharField(max_length=50) state = models.USStateField() zip = models.CharField(max_length=10, blank=True) def __str__(self): #return self.short_name return "%s (%s %s)" % (self.short_name, self.city, self.state) class Meta: verbose_name_plural = 'Addresses' class PhoneType(models.Model): description = models.CharField(max_length=20) def __str__(self): return self.description class Phone(models.Model): description = models.CharField(max_length=80) number = models.PhoneNumberField() number_type = models.ForeignKey(PhoneType) address = models.ForeignKey(Address) def __str__(self): return self.number class Date(models.Model): description = models.CharField(max_length=80) start_date = models.DateField() annual_event = models.BooleanField() address = models.ForeignKey(Address) class Note(models.Model): note_text = models.TextField() address = models.ForeignKey(Address) class Group(models.Model): description = models.CharField(max_length=80) addresses = models.ManyToManyField(Address) def __str__(self): return self.description
admin.pyは
from django.contrib import admin from appname.models import Group, Note, Date, Phone, PhoneType, Address class PhoneInline(admin.TabularInline): model = Phone extra = 3 class DateInline(admin.TabularInline): model = Date extra = 2 class NoteInline(admin.StackedInline): model = Note extra = 1 class GroupOptions(admin.ModelAdmin): filter_horizontal = ('addresses',) class AddressOptions(admin.ModelAdmin): search_fields = ['short_name', 'long_name'] fieldsets = ( ('Name', {'fields': ('short_name', 'long_name')}), ('Address', {'fields': ('addr_line_1', 'addr_line_2', 'city', 'state', 'zip')}), ) list_display = ('short_name', 'city', 'state') ordering = ('short_name',) list_filter = ('state',) inlines = [PhoneInline, DateInline, NoteInline] admin.site.register(Address, AddressOptions) admin.site.register(PhoneType) admin.site.register(Group, GroupOptions)
urls.pyも少し変更します。
admin.pyファイルを認識させるコードを1行追記するだけです。
from django.conf.urls.defaults import * # Uncomment this for admin: from django.contrib import admin from appname.admin import * urlpatterns = patterns('', # Example: # (r'^newforms/', include('newforms.foo.urls')), # Uncomment this for admin docs: #(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment this for admin: ('^admin/(.*)', admin.site.root), )
以上です。
簡単ですね。
現在最新のnewforms-adminは__str__ではなく__unicode__に変更されています。
参加メンバーの一人ymasudaさんが日経ソフトウェアで執筆したしたそうです。
それがこれ
いろいろと楽しい集まりでした。
今度はネタを提供できるようにしたい><