django-models

Django filter many to many field in admin?

雨燕双飞 提交于 2020-01-10 11:48:10
问题 I have three objects: Thing some fields Bucket things = models.ManyToManyField(Thing) User buckets = models.ManyToManyField(Bucket) things = models.ManyToManyField(Thing) When editing my "Bucket" field in the admin, I want to be able to select Things, but only those things that are in the "Things" list on the user currently logged in. How can I do that? In other words, how can I filter the list of Things in the Bucket admin? Thank you very much for your help in advance. 回答1: There is a

Reverse for 'update_comment' with arguments '('',)' not found. 1 pattern(s) tried: ['comment\\/(?P<news_pk>[0-9]+)$']

こ雲淡風輕ζ 提交于 2020-01-10 04:38:26
问题 I'm coding a news site.Now I'm detailing with the comment post function.And meet the issue says: Reverse for 'update_comment' with arguments '('',)' not found. 1 pattern(s) tried: ['comment\\/(?P<news_pk>[0-9]+)$'] I have tried many ways and times but still can't solve it and I can't find any wrong with my code.And really need your help. The comment post function is in the news_detail.html. There are two important apps in my project news and operation comment models is under operation Here is

Using strftime on a django datetime produces a UTC time in the string

强颜欢笑 提交于 2020-01-10 02:32:08
问题 I have the following code in one of my models: def shortDescription(self): return self.name + ' ' + self.class_date.strftime("%I:%M") self.class_date is a timezone aware DateTimeField , self.class_date.is_aware() is True , USE_TZ is True . The shortDescription returns a string that gives the time in UTC rather than the default timezone, putting {{ aclass.class_date }} in the template displays the time in the correct zone. Is strftime always working on the base, native time? Or what else is

Django or similar for composite primary keys

狂风中的少年 提交于 2020-01-09 18:20:52
问题 I am writing a web application for my engineering company (warning: I am a programmer only by hobby) and was planning on using Django until I hit this snag. The models I want to use naturally have multi-column primary keys. Per http://code.djangoproject.com/ticket/373, I can't use Django, at least not a released version. Can anyone help me with a workaround, whether it be via another web framework (Python-based only, please) or by suggesting changes to the model so it will work with Django's

Access ForeignKey set directly in template in Django

ぐ巨炮叔叔 提交于 2020-01-09 12:34:25
问题 I have this simplified model: class Item(models.Model): name = models.CharField(max_length=120) class ItemImage(models.Model): image = models.ImageField(upload_to='upload_dir') item = models.ForeignKey(Item) An Item can have many ItemImages . I also have a template rendering the following data set from the view: items = Item.objects.all() So now I would want to do something like this in the template: {% for item in items %} <div> {{ item.name }}<br> <img src="{{ item.itemimage_set.all()[0] }}

Django - ModelForm Create or Update? [duplicate]

南笙酒味 提交于 2020-01-09 09:08:14
问题 This question already has answers here : How to update an object from edit form in Django? (2 answers) Closed 3 years ago . I need to have a form that allows the creation or addition of sessions on a planning Model class Session(models.Model): tutor = models.ForeignKey(User) start_time = models.DateTimeField() end_time = models.DateTimeField() Form class SessionForm(forms.ModelForm): class Meta: model = Session exclude = ['tutor'] View to render the form def editor(request): if request.method

Django - ModelForm Create or Update? [duplicate]

被刻印的时光 ゝ 提交于 2020-01-09 09:07:27
问题 This question already has answers here : How to update an object from edit form in Django? (2 answers) Closed 3 years ago . I need to have a form that allows the creation or addition of sessions on a planning Model class Session(models.Model): tutor = models.ForeignKey(User) start_time = models.DateTimeField() end_time = models.DateTimeField() Form class SessionForm(forms.ModelForm): class Meta: model = Session exclude = ['tutor'] View to render the form def editor(request): if request.method

Django - ModelForm Create or Update? [duplicate]

纵饮孤独 提交于 2020-01-09 09:03:24
问题 This question already has answers here : How to update an object from edit form in Django? (2 answers) Closed 3 years ago . I need to have a form that allows the creation or addition of sessions on a planning Model class Session(models.Model): tutor = models.ForeignKey(User) start_time = models.DateTimeField() end_time = models.DateTimeField() Form class SessionForm(forms.ModelForm): class Meta: model = Session exclude = ['tutor'] View to render the form def editor(request): if request.method

How to add data into ManyToMany field?

倾然丶 夕夏残阳落幕 提交于 2020-01-09 06:05:11
问题 I can't find it anywhere, so your help will be nice for me :) Here is that field: categories = models.ManyToManyField(fragmentCategory) FragmentCategory: class fragmentCategory(models.Model): CATEGORY_CHOICES = ( ('val1', 'value1'), ('val2', 'value2'), ('val3', 'value3'), ) name = models.CharField(max_length=20, choices=CATEGORY_CHOICES) Here is the form to send: <input type="checkbox" name="val1" /> <input type="checkbox" name="val2" /> <input type="checkbox" name="val3" /> I tried something

How to add data into ManyToMany field?

百般思念 提交于 2020-01-09 06:05:05
问题 I can't find it anywhere, so your help will be nice for me :) Here is that field: categories = models.ManyToManyField(fragmentCategory) FragmentCategory: class fragmentCategory(models.Model): CATEGORY_CHOICES = ( ('val1', 'value1'), ('val2', 'value2'), ('val3', 'value3'), ) name = models.CharField(max_length=20, choices=CATEGORY_CHOICES) Here is the form to send: <input type="checkbox" name="val1" /> <input type="checkbox" name="val2" /> <input type="checkbox" name="val3" /> I tried something