I have a model like :
class MyModel(models.Model):
name = models.CharField(max_length=100)
type = models.ManyToManyField(Type, blank=True)
Here from admin I am adding MyModel.
What I want is if the type is not provided while saving then I want the type to be as default like Teacher
type Teacher has not been created. If the type is not provided I want to create the type and assign it if the type is not provided
According to documentation's example, you can override save_model like this:
#admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
def save_related(self, request, form, formsets, change):
if not form.cleaned_data['type']:
type, created = Type.objects.get_or_create(name="Teacher")
form.cleaned_data['type'] = [type]
form.save_m2m()
for formset in formsets:
self.save_formset(request, form, formset, change=change)
Related
I'm working on website whose an app which has class called Members whose a field that is related to the builtin User class from django.contrib.auth.models and it looks like
class Members(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
member_image = models.ImageField(upload_to='unknown')
member_position = models.CharField(max_length=255)
...
So as you can see when I'm adding member_image as a user I have also to select the user which doesn't make sense to me because I want to detect which user is logged in and pass his/her id as default parameter
like
class Members(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, default=request.user.id)
and after remove the user field in the admin panel like
class MembersAdmin(admin.ModelAdmin):
fields = ('member_image', 'member_position', ...)
so that if the user field doesn't selected it will set the logged in user_id by default
but to access request out of the views.py is not possible.
so how will I achieve this I also tried the following answers
Access session / request information outside of views in Django
Accessing request.user outside views.py
Django: How can I get the logged user outside of view request?, etc
but still not get it
Modify MembersAdmin save_model method and attach request.user to the object prior to saving.
class MembersAdmin(admin.ModelAdmin):
fields = ('member_image', 'member_position', ...)
def save_model(self, request, obj, form, change):
obj.user = request.user
super().save_model(request, obj, form, change)
For exclude the current logged in User for particular page or view, You can try this :-
from django.contrib.auth import get_user_model
User = user_model()
def some_view(request):
exclude_current_user = User.objects.exclude(user=request.user)
I use Django 1.7.11. I have models:
#I use django-categories app here
class Category(CategoryBase):
pass
class Advertisment(models.Model):
title = models.CharField(max_length=255, blank=True)
category = models.ForeignKey(Category, related_name='category')
all_categories = models.ManyToManyField(Category, blank=True, related_name='all_categories')
I need field "all_categories" contains "category" and all it's parent categories. I tried to use post_save, but it doesn't change any value. It even doesn't change title field. It doesn't work when I create model throught admin interface and works with custom form.
#receiver(post_save, sender=Advertisment, dispatch_uid="update_stock_count")
def update_stock(sender, instance, **kwargs):
categ = instance.category
instance.all_categories.add(categ)
for parent in categ.get_ancestors():
if parent not in instance.all_categories.all():
instance.all_categories.add(parent)
m2m_changed doesn't help too because ManyToManyField is empty and has no changes. How can I add a value from ForeignKey to ManyToMany field? What should I do in order to it works in admin interface.
I've found the solution. In admin class need to add a function save_model like this:
class AdvertismentAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
if obj.category:
category_list=[]
category = obj.category
category_list.append(category)
for parent in category.get_ancestors():
if parent not in category_list:
category_list.append(parent)
form.cleaned_data['all_categories'] = category_list
super(AdvertismentAdmin, self).save_model(request, obj, form, change)
I have a model like this:
class Tour(models.Model):
Name=models.CharField(max_length=100)
Count=models.SmallIntegerField()
ActionDate=models.DateTimeField(editable=False)
ActionUser=models.ForeignKey(User,editable=False)
StatusType=models.ForeignKey(StatusType)
now I wanna auto populate current user in my Tour,so I used this code in admin.py:
def save_model(self, request, obj, form, change):
instance = form.save(commit=False)
instance.ActionUser = request.user
instance.save()
form.save_m2m()
return instance
admin.site.register(Tour,TourAdmin)
it work great and it auto polulate current User in my Tour Table,but now I can't save current date in ActionDate field,I added this code to save_model but it cause error:
self.ActionDate=datetime.datetime.today()
then I tryed to override save in my Tour Model :
def save(self,*args,**kwargs):
self.ActionDate=datetime.datetime.today()
super(Tour, self).save(*args,**kwargs)
but this cause error,too.
what should I do to auto populate both ActionUser and ActionDate?
tnx in advance
Why don't you use auto_now in the field definition?
I have two models, a MainModel and a related InlineModel that i'd like to show as an inline in the admin. This InlineModel can be used for, say, making notes about the model and should track the logged in admin user making changes. While this seems simple (and indeed, the docs show an example for this when the user field is part of the MainModel), I can't seem to grasp it when the field is on the Inline.
To be specific, my goal is:
User edits MainModel
User adds an InlineModel, not filling in the user field
User presses save
Code fills in the user field for newly created InlineModel instances
(Bonus! user field is readonly for existing instances and hidden for new inlines)
And my questions:
Is this correct? Its too bas save_model isn't called for InlineModelAdmin instances
Does doing it this way allow me to save without causing an error? (user is required, validation flags it)
How can I hide the user input field for new inlines, and have it readonly for existing inlines?
Here are my current ideas:
#models.py
class MainModel(models.Model):
some_info = models.IntegerField()
class InlineModel(models.Model):
main = models.ForeignKey(MainModel)
data = models.CharField(max_length=255)
user = models.ForeignKey('auth.User')
#admin.py
class InlineModelInline(admin.TabularInline):
model = InlineModel
fields = ('data', 'user')
#readonly_fields = ('data', 'user') #Bonus question later
class MainModelAdmin(admin.ModelAdmin):
list_display = ('id', 'some_info')
inlines = [InlineModelInline]
#def save_model(self, request, obj, form, change):
#http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model
#Only called for MainModel, not for any of the inlines
#Otherwise, would be ideal
def save_formset(self, request, form, formset, change):
#http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_formset
#Experimenting showd this is called once per formset (where the formset is a group of inlines)
#See code block at http://code.djangoproject.com/browser/django/tags/releases/1.2.1/django/contrib/admin/options.py#L894
if not isinstance(formset.model, InlineModel):
return super(MainModelAdmin, self).save_formset(request, form, formset, change)
instances = formset.save(commit=False)
for instance in instances:
if not instance.pk:
instance.user = request.user
instance.save()
formset.save_m2m()
I have solved the first half of my question:
def save_formset(self, request, form, formset, change):
if formset.model != InlineModel:
return super(MainModelAdmin, self).save_formset(request, form, formset, change)
instances = formset.save(commit=False)
for instance in instances:
if not instance.pk:
instance.user = request.user
instance.save()
formset.save_m2m()
Now i'm interested in the bonus behavior:
I'm required to select a user when adding a new inline due to validation rules. My best guess is to not include the 'user' field in my InlineModelInline.fields tuple, but then this won't show the author for existing InlineModel instances. (Edit: adding 'user' to readonly_fields works here)
(Edit) How can I make the existing inlines render 'data' as readonly, but still be able to edit it when adding a new inline?
It worked for me. This approach won't allow me to delete Inline items.
def save_formset(self, request, form, formset, change):
for form in formset.forms:
form.instance.user = request.user
formset.save()
To answer the Bonus Question: "How can I make the existing inlines render 'data' as readonly, but still be able to edit it when adding a new inline?":
I use two inlines for the same model:
#admin.py
class InlineModelInline(admin.TabularInline):
model = InlineModel
extra = 1
max_num = 1
#admin.py
class InlineModelExistingInline(admin.TabularInline):
model = InlineModel
readonly_fields = ('data', 'user') #All Fields here except pk
can_delete = False
extra = 0
max_num = 0
class MainModelAdmin(admin.ModelAdmin):
...
inlines = [InlineModelInline, InlineModelExistingInline]
...
My form:
class PlanForm(forms.ModelForm):
owner = forms.ModelChoiceField(label="",
queryset=Profile.objects.all(),
widget=forms.HiddenInput())
etc...
class Meta:
model = Plan
Owner, in the model, is a ForeignKey to a Profile.
When I set this form, I set the value of "owner" to be a Profile object.
But when this comes out on the form, it seems to contain the name of the Profile like this:
<input type="hidden" name="owner" value="phil" id="id_owner" />
When the form is submitted and gets back to my views.py I try to handle it like this:
form = PlanForm(request.POST)
...
if form.is_valid():
plan = form.save()
return HttpResponseRedirect('/plans/%s'%plan.id) # Redirect after POST
However, what I get is a type-conversion error as it fails to turn the string "phil" (the user's name that was saved into the "owner" field) into an Int to turn it into the ForeignKey.
So what is going on here. Should a ModelForm represent a foreign key as a number and transparently handle it? Or do I need to extract the id myself into the owner field of the form? And if so, how and when do I map it back BEFORE I try to validate the form?
I suspect that the __unicode__ method for the Profile model instance, or the repr thereof is set to return a value other than self.id. For example, I just set this up:
# models.py
class Profile(models.Model):
name = models.CharField('profile name', max_length=10)
def __unicode__(self):
return u'%d' % self.id
class Plan(models.Model):
name = models.CharField('plan name', max_length=10)
profile = models.ForeignKey(Profile, related_name='profiles')
def __unicode__(self):
return self.name
# forms.py
class PlanForm(forms.ModelForm):
profile = forms.ModelChoiceField(queryset=Profile.objects.all(),
widget=forms.HiddenInput())
class Meta:
model = Plan
# views.py
def add_plan(request):
if request.method == 'POST':
return HttpResponse(request.POST['profile'])
profile = Profile.objects.all()[0]
form = PlanForm(initial={'profile':profile})
return render_to_response('add_plan.html',
{
'form':form,
},
context_instance=RequestContext(request))
With that, I see PlanForm.profile rendered thus in the template:
<input type="hidden" name="profile" value="1" id="id_profile" />
Hmm...
This might actually be a security hole.
Suppose a malicious attacker crafted a POST (say, by using XmlHttpRequest from FireBug) and set the profile term to some wacky value, like, your profile ID. Probably not what you wanted?
If possible, you may want to get the profile from the request object itself, rather than what's being submitted from the POST values.
form = PlanForm(request.POST)
if form.is_valid():
plan = form.save(commit=False)
plan.owner = request.user.get_profile()
plan.save()
form.save_m2m() # if neccesary
When you assign a Profile object to the form, Django stringifies it and uses the output as the value in the form. What you would expect though, is for Django to use the ID of the object instead.
Luckily, the workaround is simple: Just give the form primary key values of the Profile objects instead:
form = PlanForm(initial={'profile': profile.pk})
On the other end, when you're working with bound forms, however, they work much more sensibly:
form = PlanForm(request.POST)
if form.is_valid():
print form.cleaned_data['profile'] # the appropriate Profile object
There's usually no need to put related object into form field. There's a better way and this is specifying parent id in form URL.
Let's assume you need to render a form for new Plan object and then create one when form is bubmitted. Here's how your urlconf would look like:
(r"/profile/(?P<profile_id>\d+)/plan/new", view.new_plan), # uses profile_id to define proper form action
(r"/profile/(?P<profile_id>\d+)/plan/create", view.create_plan) # uses profile_id as a Plan field
And if you're changing existing object, all you need is plan_id, you can deduce any related record from it.
Since ModelChoiceField inherits from ChoiceFIeld, you should use the MultipleHiddenInput widget for this:
class PlanForm(forms.ModelForm):
owner = forms.ModelChoiceField(
queryset=Profile.objects.all(),
widget=forms.MultipleHiddenInput())
class Meta:
model = Plan