开发者

Handling an "Inventory" (complex associations) with django + appengine

开发者 https://www.devze.com 2022-12-16 11:57 出处:网络
I\'m writing a web application to manage a \"game\". Here are the models: class Character(db.Model): # Bio

I'm writing a web application to manage a "game". Here are the models:

class Character(db.Model):
    # Bio
    name = db.StringProperty()
    player = db.StringProperty()
    level = db.IntegerProperty()

class Item(db.Model):
    name = db.StringProperty()
    description = db.StringProperty()
    value = db.StringProperty()

class Inventory(db.Model):
    character = db.ReferenceProperty(Character,required=True,collection_name="inventory")
    item = db.ReferenceProperty(Item,required=True,collection_name="inventory")
    quantity = db.IntegerProperty()
equipped = db.BooleanProperty()

I'v开发者_C百科e an Item database, and when I add a character I want to manage its Inventory. I've tried ModelForms but they can't handle this sort of things. My idea is to display the complete Item list, each Item with an associated form quantity,equipped. Something like:

Sword :
  quantity ___
  equipped _
Armor :
  quantity ___
  equipped _

But, how to ship additional informations in forms?

P.S. I'm sorry that the question is dumb and not general, but I couldn't find the keywords to generalize it..


Not sure if I understand you correctly, but if you want to show the Inventory of a Character in a form and place the form of the Character at the same page, you should check out inline formsets the doc

Using inline formsets you can do something like this:

character= get_object_or_404(Character, pk=character_id)
InventoryInlineFormSet = inlineformset_factory(Character, Inventory, max_num=1)
classificationformset = ClassificationInlineFormSet(instance=character)

Out of this form you can manage your items, for example if you have a ManyToMany Relationship to your Items model you can handle them with ajax-filtered-fields link

HTH


you will want to look at inline formsets. http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets

0

精彩评论

暂无评论...
验证码 换一张
取 消