Add a column from another object to an existing listview
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have an existing listview and I want to add a new column(contactDate) from another object which will be soratble. I want to sort my LeftListView by the contact_date of the Action each Left record contains.
my objects are:
class Left(models.Model):
ref = models.CharField(max_length=16, blank=True)
customer = models.CharField(max_length=100, blank=True)
days = models.PositiveIntegerField(blank=True, null=True)
business_classification = models.CharField(max_length=15, blank=True)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
def __unicode__(self):
return u'%s in %s (%d days)' % (
self.customer,
self.days_in_delay
)
class Action(models.Model):
left = models.ForeignKey(Left, related_name='actions')
personal_use = models.ForeignKey(User, related_name='actions')
phone_calls = models.BooleanField(default=False)
contact_date = models.DateTimeField(blank=True, null=True)
description = models.CharField(max_length=100)
def __unicode__(self):
return self.description
My listview is as above:
class LeftListView(ListView):
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(LeftListView, self).dispatch(*args, **kwargs)
def get_queryset(self):
for_date = self.kwargs.get('date', datetime.date.today())
week_start, week_end = find_week_start_end(for_date=for_date)
qs_others = Left.objects.filter(created__startswith=str(for_date))
qs= qs_others #| qs_cc
reqGET = self.request.GET
@property
def querystring(self):
params = dict(self.request.GET.items())
for k, v in params.items():
if not all(v):
params.pop(k)
has_page = params.pop('page', False)
return '?%s' % urlencode(params)
def get_context_data(self, **kwargs):
ctx = super(LeftListView, self).get_context_data(**kwargs)
ctx['querystring'] = self.querystring
for_date = self.kwargs.get('date')
if for_date is not None:
ctx['for_date'] = for_date
ctx.update(search_ranges)
ctx['filter_form'] = LeftFilterForm(self.request.GET or None,
initial=search_ranges)
ctx['contact_date'] = Action.objects.filter(contact_date=for_date).filter(left_ref=left.ref)
return ctx
The result should somehow be as below:
No. REF Customer Amount Created Date Days Contact Day
1. AB-9 Name Surname amount Dec 30, 2018 68 Dec 3, 2018
2. AB-5010 Name7 Surname7 amount Mar 20, 2018 80
3. AB-78 Nametest Surnametest amount Dec 18, 2018 6
4. AB-73019 Name5 Surname5 amount Jul 08, 2018 13 Dec 3, 2018
As a newbie in python I haven't found a good example that fits my solution till now.
Any orientation is well-appreciated.
django python-2.7 listview django-1.3
|
show 4 more comments
I have an existing listview and I want to add a new column(contactDate) from another object which will be soratble. I want to sort my LeftListView by the contact_date of the Action each Left record contains.
my objects are:
class Left(models.Model):
ref = models.CharField(max_length=16, blank=True)
customer = models.CharField(max_length=100, blank=True)
days = models.PositiveIntegerField(blank=True, null=True)
business_classification = models.CharField(max_length=15, blank=True)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
def __unicode__(self):
return u'%s in %s (%d days)' % (
self.customer,
self.days_in_delay
)
class Action(models.Model):
left = models.ForeignKey(Left, related_name='actions')
personal_use = models.ForeignKey(User, related_name='actions')
phone_calls = models.BooleanField(default=False)
contact_date = models.DateTimeField(blank=True, null=True)
description = models.CharField(max_length=100)
def __unicode__(self):
return self.description
My listview is as above:
class LeftListView(ListView):
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(LeftListView, self).dispatch(*args, **kwargs)
def get_queryset(self):
for_date = self.kwargs.get('date', datetime.date.today())
week_start, week_end = find_week_start_end(for_date=for_date)
qs_others = Left.objects.filter(created__startswith=str(for_date))
qs= qs_others #| qs_cc
reqGET = self.request.GET
@property
def querystring(self):
params = dict(self.request.GET.items())
for k, v in params.items():
if not all(v):
params.pop(k)
has_page = params.pop('page', False)
return '?%s' % urlencode(params)
def get_context_data(self, **kwargs):
ctx = super(LeftListView, self).get_context_data(**kwargs)
ctx['querystring'] = self.querystring
for_date = self.kwargs.get('date')
if for_date is not None:
ctx['for_date'] = for_date
ctx.update(search_ranges)
ctx['filter_form'] = LeftFilterForm(self.request.GET or None,
initial=search_ranges)
ctx['contact_date'] = Action.objects.filter(contact_date=for_date).filter(left_ref=left.ref)
return ctx
The result should somehow be as below:
No. REF Customer Amount Created Date Days Contact Day
1. AB-9 Name Surname amount Dec 30, 2018 68 Dec 3, 2018
2. AB-5010 Name7 Surname7 amount Mar 20, 2018 80
3. AB-78 Nametest Surnametest amount Dec 18, 2018 6
4. AB-73019 Name5 Surname5 amount Jul 08, 2018 13 Dec 3, 2018
As a newbie in python I haven't found a good example that fits my solution till now.
Any orientation is well-appreciated.
django python-2.7 listview django-1.3
The FK is from Action to Left, so there are multiple Actions for each Left. Which one do you want to order by? You will need to post your view. Also note that Django 1.3 is seven years old and totally insecure, you must upgrade immediately.
– Daniel Roseman
Nov 22 '18 at 16:00
@DanielRoseman I added my listview, but I'm not sure about it. I want to sort by contact_date. I know 1.3 is a very old version but I need to do this immediate request before I upgrade the environment.
– python-Stack
Nov 22 '18 at 16:22
@BernhardBallant I saw you are familiar with django 1.3. Can you give me a hand, pls?
– python-Stack
Nov 26 '18 at 15:17
Describe what you want in the end. Not "another column" - what do you want to do with it? Are you sure you realize what 1:M relation is? SingleLeft
and bunch ofAction
s.
– Ivan Starostin
Dec 3 '18 at 10:46
@IvanStarostin I want to display in a gridview: ref, customer,days, business_classification from Left and add the contact_date from Action. Each Left has a unique ref, which contains different actions with different contact_date and I want to display, let's say, refs which has contact_date=today.I don't know if I have made myself clear at all?
– python-Stack
Dec 3 '18 at 12:59
|
show 4 more comments
I have an existing listview and I want to add a new column(contactDate) from another object which will be soratble. I want to sort my LeftListView by the contact_date of the Action each Left record contains.
my objects are:
class Left(models.Model):
ref = models.CharField(max_length=16, blank=True)
customer = models.CharField(max_length=100, blank=True)
days = models.PositiveIntegerField(blank=True, null=True)
business_classification = models.CharField(max_length=15, blank=True)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
def __unicode__(self):
return u'%s in %s (%d days)' % (
self.customer,
self.days_in_delay
)
class Action(models.Model):
left = models.ForeignKey(Left, related_name='actions')
personal_use = models.ForeignKey(User, related_name='actions')
phone_calls = models.BooleanField(default=False)
contact_date = models.DateTimeField(blank=True, null=True)
description = models.CharField(max_length=100)
def __unicode__(self):
return self.description
My listview is as above:
class LeftListView(ListView):
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(LeftListView, self).dispatch(*args, **kwargs)
def get_queryset(self):
for_date = self.kwargs.get('date', datetime.date.today())
week_start, week_end = find_week_start_end(for_date=for_date)
qs_others = Left.objects.filter(created__startswith=str(for_date))
qs= qs_others #| qs_cc
reqGET = self.request.GET
@property
def querystring(self):
params = dict(self.request.GET.items())
for k, v in params.items():
if not all(v):
params.pop(k)
has_page = params.pop('page', False)
return '?%s' % urlencode(params)
def get_context_data(self, **kwargs):
ctx = super(LeftListView, self).get_context_data(**kwargs)
ctx['querystring'] = self.querystring
for_date = self.kwargs.get('date')
if for_date is not None:
ctx['for_date'] = for_date
ctx.update(search_ranges)
ctx['filter_form'] = LeftFilterForm(self.request.GET or None,
initial=search_ranges)
ctx['contact_date'] = Action.objects.filter(contact_date=for_date).filter(left_ref=left.ref)
return ctx
The result should somehow be as below:
No. REF Customer Amount Created Date Days Contact Day
1. AB-9 Name Surname amount Dec 30, 2018 68 Dec 3, 2018
2. AB-5010 Name7 Surname7 amount Mar 20, 2018 80
3. AB-78 Nametest Surnametest amount Dec 18, 2018 6
4. AB-73019 Name5 Surname5 amount Jul 08, 2018 13 Dec 3, 2018
As a newbie in python I haven't found a good example that fits my solution till now.
Any orientation is well-appreciated.
django python-2.7 listview django-1.3
I have an existing listview and I want to add a new column(contactDate) from another object which will be soratble. I want to sort my LeftListView by the contact_date of the Action each Left record contains.
my objects are:
class Left(models.Model):
ref = models.CharField(max_length=16, blank=True)
customer = models.CharField(max_length=100, blank=True)
days = models.PositiveIntegerField(blank=True, null=True)
business_classification = models.CharField(max_length=15, blank=True)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
def __unicode__(self):
return u'%s in %s (%d days)' % (
self.customer,
self.days_in_delay
)
class Action(models.Model):
left = models.ForeignKey(Left, related_name='actions')
personal_use = models.ForeignKey(User, related_name='actions')
phone_calls = models.BooleanField(default=False)
contact_date = models.DateTimeField(blank=True, null=True)
description = models.CharField(max_length=100)
def __unicode__(self):
return self.description
My listview is as above:
class LeftListView(ListView):
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(LeftListView, self).dispatch(*args, **kwargs)
def get_queryset(self):
for_date = self.kwargs.get('date', datetime.date.today())
week_start, week_end = find_week_start_end(for_date=for_date)
qs_others = Left.objects.filter(created__startswith=str(for_date))
qs= qs_others #| qs_cc
reqGET = self.request.GET
@property
def querystring(self):
params = dict(self.request.GET.items())
for k, v in params.items():
if not all(v):
params.pop(k)
has_page = params.pop('page', False)
return '?%s' % urlencode(params)
def get_context_data(self, **kwargs):
ctx = super(LeftListView, self).get_context_data(**kwargs)
ctx['querystring'] = self.querystring
for_date = self.kwargs.get('date')
if for_date is not None:
ctx['for_date'] = for_date
ctx.update(search_ranges)
ctx['filter_form'] = LeftFilterForm(self.request.GET or None,
initial=search_ranges)
ctx['contact_date'] = Action.objects.filter(contact_date=for_date).filter(left_ref=left.ref)
return ctx
The result should somehow be as below:
No. REF Customer Amount Created Date Days Contact Day
1. AB-9 Name Surname amount Dec 30, 2018 68 Dec 3, 2018
2. AB-5010 Name7 Surname7 amount Mar 20, 2018 80
3. AB-78 Nametest Surnametest amount Dec 18, 2018 6
4. AB-73019 Name5 Surname5 amount Jul 08, 2018 13 Dec 3, 2018
As a newbie in python I haven't found a good example that fits my solution till now.
Any orientation is well-appreciated.
django python-2.7 listview django-1.3
django python-2.7 listview django-1.3
edited Dec 3 '18 at 15:02
Ivan Starostin
5,57941330
5,57941330
asked Nov 22 '18 at 15:42
python-Stackpython-Stack
13
13
The FK is from Action to Left, so there are multiple Actions for each Left. Which one do you want to order by? You will need to post your view. Also note that Django 1.3 is seven years old and totally insecure, you must upgrade immediately.
– Daniel Roseman
Nov 22 '18 at 16:00
@DanielRoseman I added my listview, but I'm not sure about it. I want to sort by contact_date. I know 1.3 is a very old version but I need to do this immediate request before I upgrade the environment.
– python-Stack
Nov 22 '18 at 16:22
@BernhardBallant I saw you are familiar with django 1.3. Can you give me a hand, pls?
– python-Stack
Nov 26 '18 at 15:17
Describe what you want in the end. Not "another column" - what do you want to do with it? Are you sure you realize what 1:M relation is? SingleLeft
and bunch ofAction
s.
– Ivan Starostin
Dec 3 '18 at 10:46
@IvanStarostin I want to display in a gridview: ref, customer,days, business_classification from Left and add the contact_date from Action. Each Left has a unique ref, which contains different actions with different contact_date and I want to display, let's say, refs which has contact_date=today.I don't know if I have made myself clear at all?
– python-Stack
Dec 3 '18 at 12:59
|
show 4 more comments
The FK is from Action to Left, so there are multiple Actions for each Left. Which one do you want to order by? You will need to post your view. Also note that Django 1.3 is seven years old and totally insecure, you must upgrade immediately.
– Daniel Roseman
Nov 22 '18 at 16:00
@DanielRoseman I added my listview, but I'm not sure about it. I want to sort by contact_date. I know 1.3 is a very old version but I need to do this immediate request before I upgrade the environment.
– python-Stack
Nov 22 '18 at 16:22
@BernhardBallant I saw you are familiar with django 1.3. Can you give me a hand, pls?
– python-Stack
Nov 26 '18 at 15:17
Describe what you want in the end. Not "another column" - what do you want to do with it? Are you sure you realize what 1:M relation is? SingleLeft
and bunch ofAction
s.
– Ivan Starostin
Dec 3 '18 at 10:46
@IvanStarostin I want to display in a gridview: ref, customer,days, business_classification from Left and add the contact_date from Action. Each Left has a unique ref, which contains different actions with different contact_date and I want to display, let's say, refs which has contact_date=today.I don't know if I have made myself clear at all?
– python-Stack
Dec 3 '18 at 12:59
The FK is from Action to Left, so there are multiple Actions for each Left. Which one do you want to order by? You will need to post your view. Also note that Django 1.3 is seven years old and totally insecure, you must upgrade immediately.
– Daniel Roseman
Nov 22 '18 at 16:00
The FK is from Action to Left, so there are multiple Actions for each Left. Which one do you want to order by? You will need to post your view. Also note that Django 1.3 is seven years old and totally insecure, you must upgrade immediately.
– Daniel Roseman
Nov 22 '18 at 16:00
@DanielRoseman I added my listview, but I'm not sure about it. I want to sort by contact_date. I know 1.3 is a very old version but I need to do this immediate request before I upgrade the environment.
– python-Stack
Nov 22 '18 at 16:22
@DanielRoseman I added my listview, but I'm not sure about it. I want to sort by contact_date. I know 1.3 is a very old version but I need to do this immediate request before I upgrade the environment.
– python-Stack
Nov 22 '18 at 16:22
@BernhardBallant I saw you are familiar with django 1.3. Can you give me a hand, pls?
– python-Stack
Nov 26 '18 at 15:17
@BernhardBallant I saw you are familiar with django 1.3. Can you give me a hand, pls?
– python-Stack
Nov 26 '18 at 15:17
Describe what you want in the end. Not "another column" - what do you want to do with it? Are you sure you realize what 1:M relation is? Single
Left
and bunch of Action
s.– Ivan Starostin
Dec 3 '18 at 10:46
Describe what you want in the end. Not "another column" - what do you want to do with it? Are you sure you realize what 1:M relation is? Single
Left
and bunch of Action
s.– Ivan Starostin
Dec 3 '18 at 10:46
@IvanStarostin I want to display in a gridview: ref, customer,days, business_classification from Left and add the contact_date from Action. Each Left has a unique ref, which contains different actions with different contact_date and I want to display, let's say, refs which has contact_date=today.I don't know if I have made myself clear at all?
– python-Stack
Dec 3 '18 at 12:59
@IvanStarostin I want to display in a gridview: ref, customer,days, business_classification from Left and add the contact_date from Action. Each Left has a unique ref, which contains different actions with different contact_date and I want to display, let's say, refs which has contact_date=today.I don't know if I have made myself clear at all?
– python-Stack
Dec 3 '18 at 12:59
|
show 4 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434333%2fadd-a-column-from-another-object-to-an-existing-listview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434333%2fadd-a-column-from-another-object-to-an-existing-listview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The FK is from Action to Left, so there are multiple Actions for each Left. Which one do you want to order by? You will need to post your view. Also note that Django 1.3 is seven years old and totally insecure, you must upgrade immediately.
– Daniel Roseman
Nov 22 '18 at 16:00
@DanielRoseman I added my listview, but I'm not sure about it. I want to sort by contact_date. I know 1.3 is a very old version but I need to do this immediate request before I upgrade the environment.
– python-Stack
Nov 22 '18 at 16:22
@BernhardBallant I saw you are familiar with django 1.3. Can you give me a hand, pls?
– python-Stack
Nov 26 '18 at 15:17
Describe what you want in the end. Not "another column" - what do you want to do with it? Are you sure you realize what 1:M relation is? Single
Left
and bunch ofAction
s.– Ivan Starostin
Dec 3 '18 at 10:46
@IvanStarostin I want to display in a gridview: ref, customer,days, business_classification from Left and add the contact_date from Action. Each Left has a unique ref, which contains different actions with different contact_date and I want to display, let's say, refs which has contact_date=today.I don't know if I have made myself clear at all?
– python-Stack
Dec 3 '18 at 12:59