开发者

Django planmember_set issue

开发者 https://www.devze.com 2023-04-08 03:34 出处:网络
I have a question that I hope someone can help me with.I\'m using a form to have the user select a \"plan\".Now I want to list all the members in the plan, in the form.I have a function \'get_owners\'

I have a question that I hope someone can help me with. I'm using a form to have the user select a "plan". Now I want to list all the members in the plan, in the form. I have a function 'get_owners' that grabs all the members in the plan, by using this function:

def get_owners(self):
    owners = self.planmember_set.filter(ownership_type__code__in=["primary","joint"])
    return owners

Only problem is that I get an output of:

[<PlanMember: Doe, John (Primary)>, <PlanMember: Doe, Jane(Joint Subscriber)>, etc]

Now is there a way for me to just show their names and the member type (the words in brackets)?

A bit of background code.

Here is my views.py:

if request.POST:
    form = PlanMaturityYearForm(Plan.objects.all().filter(profile = p), request.POST)
    if form.is_valid():
        selected_plan = form.cleaned_data['plans']
        plan = get_object_or_404(Plan, pk=selected_plan.pk)
        investment_list = Investment.objects.all().filter(Q(plan = plan)).order_by('maturity_date')
        context['investment_list'] = investment_list
        context['plan'] = plan
        context['today'] = now
        context['current_yr'] = current_year
        context['next_yr'] = next_year
        context['show_report'] = True
else:
    form = PlanMaturityYearForm(Plan.objects.all().filter(profile = p))
context['form'] = form
return render_to_response('reports/planmaturities_year.html', RequestContext(request, context))

And my forms.py:

class PlanMaturityYearForm(forms.Form):
def __init__(self, plans,  *args, **kwargs):

    super(PlanMaturityYearForm, self).__init__(*args, **kwargs)
    self.fields['plans'] = forms.ModelChoiceField(
        plans,
        required=True,
        widget=forms.Select(attrs={'size': 20}),
        error_messages={'required':'Please select the plan you wish to build the report for',}
    )

Edit:

models.py

class Plan(models.Model):
old_id = models.IntegerField(null=True)
closed = models.BooleanField(default=False, blank=True)
closed_date = model开发者_Python百科s.DateField(null=True)
profile = models.ForeignKey(Profile, default=False)
plan_type = models.ForeignKey(PlanType)
ownership_type = models.ForeignKey(OwnershipType)
status = models.PositiveSmallIntegerField(max_length=2, default=PLAN_OPEN)
timestamp = models.DateTimeField(auto_now_add=True, null=True, blank=True)
notes = HTMLField(blank=True, null=True)
bank = models.CharField(max_length=50, blank=True, null=True)
account = models.CharField(max_length=50, blank=True, null=True)
transit = models.CharField(max_length=50, blank=True, null=True)
objects = PlanManager()

class Profile(models.Model):
old_id = models.IntegerField(null=True, blank=True)
label = models.CharField(max_length=255, blank=True, verbose_name='Profile label/description')
location = models.ForeignKey(Location, null=True, blank=True)
client = models.BooleanField(default=False, blank=True)
corp = models.BooleanField(default=False, blank=True)
noncorp = models.BooleanField(default=False, blank=True)
timestamp = models.DateTimeField(auto_now_add=True, default=False)
bak_agent_id = models.CharField(max_length=10, null=True, blank=True)
agent = models.ForeignKey(Agent, related_name='profiles')
check1 = models.BooleanField(default=False, blank=True)
incorp_date = models.DateField(null=True, blank=True)
corp_records = models.BooleanField(default=False, blank=True)
articles_of_incorp = models.BooleanField(default=False, blank=True)
other_corp_articles = models.CharField(max_length=50, null=True, blank=True)
bin = models.CharField(max_length=50, null=True, blank=True, verbose_name='BIN')
phone = models.CharField(max_length=25, null=True)
registration = models.CharField(max_length=45, null=True, blank=True)
num_sigs = models.IntegerField(max_length=1, null=True, blank=True,
        verbose_name='Number of signatures required to transaction the account')
charity = models.BooleanField(default=False, blank=True)
donations_solicited = models.BooleanField(default=False, blank=True)
ownership_type = models.CharField(max_length=45, null=True, blank=True)
ownership_other = models.CharField(max_length=45, null=True, blank=True) #work around for other business type selction
business_nature = models.CharField(max_length=45, null=True, blank=True)
date_incept = models.DateField(null=True, blank=True)
bind_power = models.BooleanField(default=False, blank=True)
reg_documents = models.BooleanField(default=False, blank=True)
other_documents = models.CharField(max_length=50, null=True, blank=True)
reg_number = models.CharField(max_length=45, null=True, blank=True)
tax_number = models.CharField(max_length=45, null=True, blank=True)
phone = models.CharField(max_length=25, null=True, blank=True)
fax = models.CharField(max_length=25, null=True, blank=True)
email = models.CharField(max_length=100, null=True, blank=True)


You can use values_list:

owners = self.planmember_set.filter(
             ownership_type__code__in=["primary","joint"]
         ).values_list('name', 'type')


def get_owners(self):
    owners = self.planmember_set.filter(ownership_type__code__in=["primary","joint"]).values_list('name_field', 'ownership_type__code')
    return owners

Write where and how you use get_owners if not correct.

0

精彩评论

暂无评论...
验证码 换一张
取 消