Ok I got it to work with the following code. Just wondering if there is
another way to do this as I really don't like adding to the request object
to get it to work.
class RecommendationInline(admin.StackedInline):
model = Recommendation
def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
field = super(RecommendationInline,
self).formfield_for_foreignkey(db_field, request, **kwargs)
if db_field.name == 'workexperience':
if request.__obj__ is not None:
field.queryset = field.queryset.filter(user__exact =
request.__obj__)
else:
field.queryset = field.queryset.none()
return field
class CustomUser(UserAdmin)
...
def get_form(self, request, obj=None, **kwargs):
request.__obj__ = obj
return super(CustomUserAdmin, self).get_form(request, obj, **kwargs)
On Thursday, December 6, 2012 1:41:21 PM UTC-8, Detectedstealth wrote:
>
> Hi Andrew,
>
> I think you are right the queryset is a must. However how do I find the
> selected user inside the queryset, I don't want the logged in user. For
> example if I go to edit user ID 2 in the queryset I need to know that id so
> I can change the filter to find experiences only with that ID.
>
> IE:
> class Recommendation(admin.StackedInline):
> model = Recommendation
>
> def queryset(self, request)
> qs = super(RecommendationInline, self).queryset(request)
> # now the code is already pulling the correct recommendations for
> the user, however the recommendations model has
>
> workexperience = models.ForeignKey(WorkExperience, verbose_name=_('work
> experience')) this is the field I need to filter based on the selected
> user ID. So the drop down in admin only gives the ability to add a
> recommendation to a work experience the selected user has linked to his/her
> profile.
> This might sound more complicated then it really is here is a better
> example of how I would like things to work:
>
> 1) Visit edit page for: Bruce Wade
> 2) Can view and add work experience for Bruce Wade on the edit profile page
> 3) Can view and add any recommendations linked to any work experience
> Bruce Wade has entered in step to. Because I need to select which work
> experience I want to add the recommendation for, there is a drop down for
> the foreignkey work experience. However it is listing everyone's work
> experience not just Bruce Wade's work experience to select from.
>
> Another idea (which I have no idea if it is possible) have recommendations
> inlined inside of work experience and have work experience inlined inside
> of profile.
>
> - Bruce
>
> On Thursday, December 6, 2012 12:30:13 AM UTC-8, Andrew Macgregor wrote:
>>
>> Hi,
>>
>> I think you may need to add a queryset() method on your InlineModelAdmin
>> and filter by user.
>>
>>
>> https://docs.djangoproject.com/en/dev/ref/contrib/admin/#inlinemodeladmin-options
>>
>> You can use the example from ModelAdmin:
>>
>>
>> https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.queryset
>>
>> - Andrew.
>>
>>
>>
>> On Thursday, December 6, 2012 2:50:43 PM UTC+8, Detectedstealth wrote:
>>>
>>> See the attachment.
>>>
>>> The dropdown is showing a work experience that was added by a different
>>> user. The dropdown should only show work experience that was entered for
>>> the user that is currently being edited.
>>>
>>> The purpose is to be able to add/edit recommendations that are linked to
>>> any work experience the selected user has entered.
>>>
>>> On Wednesday, December 5, 2012 9:38:37 PM UTC-8, Chris Cogdon wrote:
>>>>
>>>> When you say "recommendations inline lists work history from all users"
>>>> do you mean its showing up as multiple fields, or do you mean the
>>>> selection
>>>> box that gives you the option of adding a recommendation from the list of
>>>> possible recommendations ?
>>>>
>>>>
>>>> On Wednesday, December 5, 2012 8:06:55 PM UTC-8, Detectedstealth wrote:
>>>>>
>>>>> Hi,
>>>>>
>>>>> I have a custom user who has work experience, and for each work
>>>>> experience there can be multiple recommendations for the work experience.
>>>>>
>>>>> My models:
>>>>> CustomUser
>>>>>
>>>>> class WorkExperience(models.Model):
>>>>>
>>>>> user = models.ForeignKey(CustomUser, verbose_name=_('user profile'
>>>>> ))
>>>>>
>>>>> company = models.CharField(_('company name'), max_length=150)
>>>>>
>>>>> location = models.CharField(_('job location'), max_length=150)
>>>>>
>>>>> position = models.CharField(_('position'), max_length=150)
>>>>>
>>>>> start_date = models.DateField(_('started position'))
>>>>>
>>>>> end_date = models.DateField(_('finished position'), null=True,
>>>>> blank=True)
>>>>>
>>>>> description = models.TextField(_('details'), help_text=_('Write a
>>>>> short description about your role and experience when at this position.'
>>>>> ))
>>>>>
>>>>>
>>>>>
>>>>> last_edited = models.DateTimeField(default=timezone.now)
>>>>>
>>>>> date_added = models.DateTimeField(default=timezone.now)
>>>>>
>>>>>
>>>>>
>>>>> def __unicode__(self):
>>>>>
>>>>> return "%s - %s" % (self.company, self.position)
>>>>>
>>>>>
>>>>>
>>>>> class Recommendation(models.Model):
>>>>>
>>>>> user = models.ForeignKey(CustomUser, verbose_name=_('recommended
>>>>> user'))
>>>>>
>>>>> workexperience = models.ForeignKey(WorkExperience,
>>>>> verbose_name=_('work
>>>>> experience'))
>>>>>
>>>>> details = models.TextField(_('recommendation'))
>>>>>
>>>>> approved = models.BooleanField(default=False)
>>>>>
>>>>> is_spam = models.BooleanField(default=False)
>>>>>
>>>>> date_added = models.DateTimeField(default=timezone.now)
>>>>>
>>>>> admin.py
>>>>>
>>>>> class ExperienceInline(admin.StackedInline):
>>>>>
>>>>> model = WorkExperience
>>>>>
>>>>> fieldsets = (
>>>>>
>>>>> (None, {
>>>>>
>>>>> 'fields': (
>>>>>
>>>>> 'company',
>>>>>
>>>>> 'position',
>>>>>
>>>>> 'location',
>>>>>
>>>>> ('start_date','end_date'),
>>>>>
>>>>> 'description'
>>>>>
>>>>> )
>>>>>
>>>>> }),
>>>>>
>>>>> )
>>>>>
>>>>> class RecommendationInline(admin.StackedInline):
>>>>>
>>>>> model = Recommendation
>>>>>
>>>>> class CustomUserAdmin(UserAdmin):
>>>>>
>>>>> ...
>>>>> inlines = [ExperienceInline, RecommendationInline]
>>>>>
>>>>>
>>>>> The problem is the recommendations inline lists work history from all
>>>>> users instead of showing only the work experience from the selected user.
>>>>> Is it possible to list only work experience belong to the selected user?
>>>>>
>>>>>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/django-users/-/pKZgCW28Nj8J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.