I have a Profile model that has OneToOne relationship with User model.
I also have a Group model that has users field as ManyToMany to User.
I am trying to achieve a simple thing in Django Admin: being able to create a group with some new users and allowing to fill out some of the profile fields for those new users.
I tried a couple of things
class ProfileInline(admin.StackedInline):
model = Profile
class UserInline(admin.StackedInline):
model = Group.users.through
inlines = [ ProfileInline ]
class GroupAdmin(admin.ModelAdmin):
inlines = [
UserInline,
]
exclude = ('users',)
This is not working for me. I only see dropdown fields for user in my group admin and if I try to add a user the form does not have any profile fields.
You can't do that. Django admin inlines are only available if model has foreign key to other model. For you setup you have to find other solution related to your models relation.
Related
I want to change in my django administration page the form of a field. In my model I have the 'user' as foreign key to another model. When I create a new 'programare' in the administration page the default form-field for user it's a dropdown with all entries of 'Cont' model. It's dizzy to search the right one, so I want to change that dropdown with a plain-text input or an autocomplete form.
class Programare(models.Model):
user = models.ForeignKey(to=Cont, on_delete=models.CASCADE, related_name='programari')
...
I found the answer for my question. For django ^2.0 admin
class ProgramareAdmin(admin.ModelAdmin):
model = Programare
autocomplete_fields = ['user']
admin.site.register(Programare, ProgramareAdmin)
I need use inlines in django admin for show relation between two models but in the moment that i do, i had to do the reverse relationship to show inlines.
Example:
class OtherModel(models.Model):
field1=models...
........
class Model(models.Model)
field1 = models....
other_model = models.ForeignKey(OtherModel)
I create the inline...
class OtherModelInline(admin.StackedInline):
model = OtherModel
extra = 1
#admin.register(Model):
class ModelAdmin(admin.modelAdmin):
inlines = [OtherModelInline]
So...
When I create the Inline it required foreign key on OtherModel..
How can I show this without change the relationship?
This is the right way to do it.
If you want to use inlines, you need to specify that these two models are somewhat related.
I have the following situation: A user can have more than one profile. Here is my models class. Something like this example:
models.py:
class Profile:
name=models.Charfield()
class UserProfile:
user=models.ForeignKey(User)
profile = models.ForeignKey(Profile)
serializers.py:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = '__all__'
Here I'm returning all my users by JSON but I would like to add a new field called profiles that returns all ids profiles that the user have.
{ "id": 1,
"name" : "John"
....
profiles = [1, 2]
}
How can I get(query) all profiles that the user have and add them on my final JSON?
on the UserSerializer declare the field profile as PrimaryKeyRelatedField
profiles = serializers.PrimaryKeyRelatedField()
By default this field is read-write, although you can change this behavior using the read_only flag.
see docs
1) Simplify relations - only User and Profile models needed in this case, you don't have to store explicit relation in UserProfile table as the same could be done in Profile model, as long as you don't need Users to share profiles with other Users.
2) Create ProfileSerializer
3) Add profiles field to UserSerializer with many=True property & provide reference in 'source' property
Please reffer to these docs as they are really good
http://www.django-rest-framework.org/api-guide/serializers/
Another thing to mention, creating UserProfile is depricated and in new versions of Django you can extend basic User model using setting property AUTH_USER_MODEL
The Django admin panel has a search autocomplete with search_fields on the list of objects of a model, but right now I have 3000 users. To add a user manually is dificult with the selectbox; I need the same behavior like the searchfields for selecting a user foreinkey User.
How can I include the Django search feature on the form for editing inside the admin panel?
from myapp.models import Red
from django.contrib.auth.models import User
class Red(models.Model):
customer = models.ForeignKey(User, verbose_name="Cliente")
pub_date = models.DateTimeField(default=datetime.now, blank=True)
Since Django 2.0, you can use autocomplete_fields to generate autocomplete fields for foreign keys.
class UserAdmin(admin.ModelAdmin):
search_fields = ['username', 'email']
class RedAdmin(admin.ModelAdmin):
autocomplete_fields = ['customer']
Django has no built-in autocomplete functionality for foreign keys on admin but the raw_id_fields option may help:
class RedAdmin(admin.ModelAdmin):
raw_id_fields = ("customer", )
If you want real autocomplete then you have to use 3rd-party app like django-autocomplete-light or some of the other solutions.
Is there any way to use a Form Wizard in Admin interface for add/edit Models.
(using Django 1.5.2)
for example:
--models.py--
class AModel(models.Model):
fieldA = models.CharField(max_length=64)
fieldB = models.CharField(max_length=64)
--admin.py--
class Form1(ModelForm):
class Meta:
model = AModel
fields = ('fieldA',)
class Form2( ModelForm ):
class Meta:
model = AModel
fields = ('fieldB',)
.... something add for make this two forms in one multipage admin form , is that possible? or any other way to do the same job.
Thanks in advance.
It isn't possible by using admin module. The only way is either some external plugins or you need to create custom admin views. Google around..