0

When I tried the above suggestion by Daniel Roseman and Tim Diggins at Redirect on admin Save, if I put in a URL and specified the obj.id, it worked, but when I used obj.id it would give an error that Django could not find "None". To get around this, I had to find the max value of the id in my model and use it (as shown below).

def response_add(self, request, obj, post_url_continue=None):
    arunidlist = arun.objects.values_list('id', flat=True)
    newpk = max(arunidlist)
    return HttpResponseRedirect("/admin/nbig/arun/%s" % str(newpk)) 
    # this did not work: return HttpResponseRedirect("../%s" % obj.id)        
    # this worked:   return HttpResponseRedirect("/admin/nbig/arun/99")

(note that nbig is my model and arun is my app)

I see that the string I referenced was a couple of years old, but if anyone can tell me how to avoid the hackiness, I would appreciate it. Thanks.

Edited question:

Revised function:

def response_add(self, request, obj, post_url_continue=None):
    return HttpResponseRedirect("../%s" % obj.id)

Model (Django created the ID primary key):

class arun(models.Model):
    auser = models.CharField(max_length=15, null=True)
    more fields here
    alastcompid = models.FloatField(null=True)
    class Meta:
        verbose_name = 'NBI Graph'
        verbose_name_plural = 'NBI Graph'
    def __str__(self): 
        return str(self.id)

Error: Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/admin/nbig/arun/None/ Raised by: django.contrib.admin.options.change_view NBI Graph object with primary key 'None' does not exist. You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

Relevant parts of my save as new code (could this be causing the problem?):

    def save_model(self, request, obj, form, change):
        f_id = request.POST.get('id','') 
        f_adate = datetime.now().date()     
        f_atime = datetime.now().time() 

        if '_saveasnew' in request.POST:
            aa = arun(id=f_id, adate = f_adate, atime = f_atime)
            aa.save(force_insert=True)
Community
  • 1
  • 1
Sandra
  • 137
  • 13
  • 1
    Have you manually specified the primary key on the model? Can you show your model, and the full traceback when you use `obj.id`. – Alasdair Jan 22 '16 at 15:02
  • Alasdair, Thanks for any help you can provide. I do not manually set the id field. Revised function: def response_add(self, request, obj, post_url_continue=None): return HttpResponseRedirect("../%s" % obj.id) Model: class arun(models.Model): auser = models.CharField(max_length=15, null=True) more fields here alastcompid = models.FloatField(null=True) class Meta: verbose_name = 'NBI Graph' verbose_name_plural = 'NBI Graph' def __str__(self): return str(self.id) – Sandra Jan 22 '16 at 15:54
  • Here is the error message: Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/admin/nbig/arun/None/ Raised by: django.contrib.admin.options.change_view NBI Graph object with primary key 'None' does not exist. – Sandra Jan 22 '16 at 15:54
  • Please [edit] your question and add the code and error message there -- the formatting makes it much easier to read! – Alasdair Jan 22 '16 at 15:55
  • Thanks for pointing me to the edit feature. I updated my question. – Sandra Jan 22 '16 at 16:20
  • (note that nbig is my model and arun is my app) <-- you mean that nbig is your app and arun is your model, right? – MariusSiuram Jan 22 '16 at 16:24
  • MariusSiuram - yes. Sorry. – Sandra Jan 22 '16 at 16:27
  • Could you put your full fields of the model? I fail to replicate your issue... if you can provide an [mcve](http://stackoverflow.com/help/mcve) it would be great – MariusSiuram Jan 22 '16 at 16:29
  • I have a lot of code in my def save_model; maybe I am breaking the relationship to the obj.id. In the saveasnew section, I force the insert of the row into my arun model. Could that be causing the problem? – Sandra Jan 22 '16 at 16:33
  • I added some of my code to the original question. – Sandra Jan 22 '16 at 16:39
  • Is that all of your `save_model` method? You are not handling the case where `_saveasnew` is not in `request.POST`. – Alasdair Jan 22 '16 at 16:47
  • Alasdair, it is really long, but, I specifically have a _saveasnew and a _continue. I have also be trying to add a second _continue button (_continu2) that will use some of the form values and make some changes to my model, then return to the change_form, like the _continue button does, but I can not get it work (it makes the changes, but takes me back to the changelist - I posted a different question about this issue). I do not have an "else" condition, so, maybe that is the problem, but I thought that the response_add was called when you are doing the saveasnew. No? – Sandra Jan 22 '16 at 17:09
  • You have a very clear understanding of your example in your head, but I feel like everybody else (we) are failing to see it. Occam's razor suggests this: if obj.id seems to be None, then it means that it is None. If that is because you are doing some `saveasnew` which fails to populate the `id` / `pk` field, or because the admin is creating a new object, or whatever, that we cannot know (because you are not providing it). Start with a minimal case (which will work or not), and then start to grow. Ask when things break. – MariusSiuram Jan 25 '16 at 09:35
  • MariusSiuram, Thank your for sharing your thoughts regarding starting with a minimal case and growing. I often use that method when programming, and also when testing new parts of a program. These two issues (the obj.id and the second continue button) are things that came up when I was trying to modify an existing application. The application is extensive and posting all of it would not have been feasible. Thanks again for your time in contributing to my question. – Sandra Jan 25 '16 at 13:41

1 Answers1

0

The save_model method expects you save the obj that is passed in. You shouldn't create a different object instead.

You get the error when access obj.id because you haven't saved obj.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Alasdair, So when I force the save I am essentially overriding Django's handling of the save and breaking its connection to the obj.id? That makes sense. I forced the save because I was trying to control the form fields (I have several form widgets too), and I could not figure out how to get the admin to do the save by default. Thanks for your help. – Sandra Jan 25 '16 at 13:34
  • The problem isn't that you are calling`save(force_insert=True)`, the problem is that you are saving a new object `aa` instead of the object `obj`. If you want, you can change `obj`'s values before saving, e.g. `obj.f_adate = new_value`. – Alasdair Jan 25 '16 at 14:33
  • Oh...okay. Thanks for explaining again. I totally misunderstood the first time. I will try that. – Sandra Jan 25 '16 at 14:42
  • After the force save, I added this: obj.id=f_id, and now it works. Thanks so much for explaining this! – Sandra Jan 25 '16 at 15:08
  • Glad it works, but ideally should be calling `obj.save()`, then Django will set the id for you. Setting `obj.id=f_id` sounds fragile to me. – Alasdair Jan 25 '16 at 15:11