Multiple stackinline all sharing relation need assistance

60 views
Skip to first unread message

Detectedstealth

unread,
Dec 5, 2012, 11:06:55 PM12/5/12
to django...@googlegroups.com
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?

Chris Cogdon

unread,
Dec 6, 2012, 12:38:37 AM12/6/12
to django...@googlegroups.com
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 ?

Detectedstealth

unread,
Dec 6, 2012, 1:50:43 AM12/6/12
to django...@googlegroups.com
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.
recommendation_inline.png

Andrew Macgregor

unread,
Dec 6, 2012, 3:30:13 AM12/6/12
to django...@googlegroups.com
Hi,

I think you may need to add a queryset() method on your InlineModelAdmin and filter by user.


You can use the example from ModelAdmin:


- Andrew.

Detectedstealth

unread,
Dec 6, 2012, 4:41:21 PM12/6/12
to django...@googlegroups.com
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

Detectedstealth

unread,
Dec 6, 2012, 5:24:23 PM12/6/12
to django...@googlegroups.com
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)
Reply all
Reply to author
Forward
0 new messages