django-models

How to show all fields of model in admin page?

落爺英雄遲暮 提交于 2020-05-24 08:44:48
问题 here is the models page In this picture, only the title shows up on here, I used: def __unicode__(self): return self.title; here is the each individual objects How do I show all these fields? How do I show all the fields in each Model page? 回答1: By default, the admin layout only shows what is returned from the object's unicode function. To display something else you need to create a custom admin form in app_dir/admin.py . See here: https://docs.djangoproject.com/en/dev/ref/contrib/admin/

Update many-to-many field in django with field name in variable

人走茶凉 提交于 2020-05-18 05:28:08
问题 I want to be able to update a many-to-many field on a model, when the name of the field is stored in a variable. This is relatively easy for a normal field (with kwargs in the constructor), or even a foreign key, but not so for a many-to-many field. Here's the situation: class Book(models.Model): title = models.CharField(max_length=30) authors = models.ManyToManyField(Author) field_name = 'title' new = Book(**{field_name:'My Book'}) # This sets title to mybook new.save() my_authors = Author

Can't get the detail view of a post in a social network project

二次信任 提交于 2020-05-17 07:15:06
问题 I am building a simple social network in django. In the "home" of my social, I have the list of all posts published by all users, with author and publishing date. The publishing date have a link to a url that should return the detail view of a specific post published by a user. However, as I click on it, it shows me the list of all posts published by its author (this also works when I click on the author link of the post). So both www.mysocial.com/posts/by/ciccio/7/ and www.mysocial.com/posts

Adding a user to a group Django Rest Framework

余生长醉 提交于 2020-05-17 05:56:51
问题 I want to use an API to create a user and add it to a selected group. But when I execute the POST request with Postman I get an error saying this group already exists, I don't want to make a new group. I just want to add the user to it. POST body This is what I send in the post. { "email": "user@example.com", "username": "Laerie", "first_name": "Laerie", "last_name": "koek", "password": "password", "groups": [{"name":"user"}] } Response I keep getting this error { "groups": [ { "name": [

Update instances of ModelFormset

筅森魡賤 提交于 2020-05-16 01:21:08
问题 In my django app two models are connecetd by manytomany relation and I am using modelformset_fatory to create a form like this Views.py def post(request): tform = TeamForm() pform = modelformset_factory(Player, form=PlayerForm, extra = 1) pform = pform(request.POST or None, queryset = Player.objects.filter(id__isnull = True)) if request.method == 'POST': t = Team() tform = TeamForm(request.POST, instance=t) if tform.is_valid() and pform.is_valid(): tform.save() instances = pform.save(commit

Add multiple models and custom fields to a json response in Django Rest Framework

白昼怎懂夜的黑 提交于 2020-05-15 08:09:15
问题 i'm new into Python/Django programming and i got stuck with something in a personal project that i'm doing. My issue is that i want to return a custom response based on different models of my application, some of the values will come from custom queries and others are part of the models itself. So, i have the following models in my app(some fields were deleted to not make the post too long): class Parking(models.Model): google_id = models.CharField(max_length=100) short_name = models

How to filter ModelAdmin autocomplete_fields results with the context of limit_choices_to

佐手、 提交于 2020-05-15 08:04:34
问题 I have a situation where I wish to utilize Django's autocomplete admin widget, that respects a referencing models field limitation. For example I have the following Collection model that has the attribute kind with specified choices. class Collection(models.Model): ... COLLECTION_KINDS = ( ('personal', 'Personal'), ('collaborative', 'Collaborative'), ) name = models.CharField() kind = models.CharField(choices=COLLECTION_KINDS) ... Another model ScheduledCollection references Collection with a

How to filter ModelAdmin autocomplete_fields results with the context of limit_choices_to

痴心易碎 提交于 2020-05-15 08:01:06
问题 I have a situation where I wish to utilize Django's autocomplete admin widget, that respects a referencing models field limitation. For example I have the following Collection model that has the attribute kind with specified choices. class Collection(models.Model): ... COLLECTION_KINDS = ( ('personal', 'Personal'), ('collaborative', 'Collaborative'), ) name = models.CharField() kind = models.CharField(choices=COLLECTION_KINDS) ... Another model ScheduledCollection references Collection with a

Control object creation flow in Django inheritance models

♀尐吖头ヾ 提交于 2020-05-15 07:19:51
问题 I have been read some Django document about inheritance models and parent_link . Suppose that I have these models: class Parent(models.Model): #Some field goes here! class Child(Parent): #Some field goes here! I have 3 questions about this pattern: What should I do if I want to create new child object and pass id of existing parent to that? What should I do if I want to create just new child object and after a while create parent object for that child? Also I din't understand this document

Control object creation flow in Django inheritance models

大憨熊 提交于 2020-05-15 07:19:45
问题 I have been read some Django document about inheritance models and parent_link . Suppose that I have these models: class Parent(models.Model): #Some field goes here! class Child(Parent): #Some field goes here! I have 3 questions about this pattern: What should I do if I want to create new child object and pass id of existing parent to that? What should I do if I want to create just new child object and after a while create parent object for that child? Also I din't understand this document