Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Dec. 7, 2005 17:43:34

P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

ID of the record


Hi,
Is it possible to find out the ID of the record being inserted?
To explain what I mean:
Let's suppose the example athttp://www.djangoproject.com/documentation/forms/####################################
def create_place(request):
manipulator = places.AddManipulator()
if request.POST:
# If data was POSTed, we're trying to create a new Place.
new_data = request.POST.copy()
# Check for errors.
errors = manipulator.get_validation_errors(new_data)
if not errors:
# No errors. This means we can save the data!
## IS IT POSSIBLE HERE TO FIND OUT the ID of record that
will consist of the data?
manipulator.do_html2python(new_data)
new_place = manipulator.save(new_data)

# Redirect to the object's "edit" page. Always use a
redirect
# after POST data, so that reloads don't accidently create
# duplicate entires, and so users don't see the confusing
# "Repost POST data?" alert box in their browsers.
return HttpResponseRedirect("/places/edit/%i/" %
new_place.id)
else:
# No POST, so we want a brand new form without any data or
errors.
errors = new_data = {}

# Create the FormWrapper, template, context, response.
form = formfields.FormWrapper(manipulator, new_data, errors)
return render_to_response('places/create_form', {'form': form})
#############
and on line 10 I marked where I would like to find out the record id.
So, is it possible?
Thank you for reply
l.

Offline

#2 Dec. 7, 2005 19:14:46

k.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

ID of the record


Could you explain why you want the id of the record at that point? The
record doesn't actually exist at that point, so it doesn't have an id.
The id is generated when manipulator.save is called. At that point you
have a copy of the object so you can just do new_place.id to get the
id.

Kieran

Offline

#3 Dec. 7, 2005 19:43:19

oggie r.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

ID of the record


I did a similar thing not long ago. I used '/places/edit/%s/' %
new_place.id (the difference being %s vs %i), but I don't think that
matters.

Also (and I'm not sure how much this matters), rjwittams suggested in a
recent change that the do_html2python() call should occur before the
"if errors:" test.

So what is the problem with what you're trying?

-rob

Offline

#4 Dec. 8, 2005 07:23:34

P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

ID of the record


Thank you Kieran and Rob for your reply. The reason why I want to know
id of the record is that I would like to name a picture by the id
number( for example the picture will be 4330.jpg if the record id is
4330) And then the picture is linked with the record data.I would like
to have an image( picture) separately from the database so I think that
it is a solution(to link image id_number.jpg with the table record).
Or is it a wrong idea?
Thank you for your reply
Regards,
L.

Offline

#5 Dec. 8, 2005 11:28:51

k.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

ID of the record


PythonistL wrote:
> Thank you Kieran and Rob for your reply. The reason why I want to know
> id of the record is that I would like to name a picture by the id
> number( for example the picture will be 4330.jpg if the record id is
> 4330) And then the picture is linked with the record data.I would like
> to have an image( picture) separately from the database so I think that
> it is a solution(to link image id_number.jpg with the table record).
> Or is it a wrong idea?

I think that you probably want the standard FileField or ImageField
described here:http://www.djangoproject.com/documentation/model_api/These fields do not actually store the file/image in the database, just
the name of the file. You need to set MEDIA_ROOT/MEDIA_URL as
described in the documentation so that Django knows where to save the
file to disk. If for some reason you want to go back from filename to
model then you can do a query based on the filename, remembering that
the path you are querying for is relative to MEDIA_ROOT.

e.g. mymodels.get_object(myfilefield__exact="myimage.jpg")

Kieran

Offline

#6 Dec. 9, 2005 10:11:00

P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

ID of the record


Thank you for the reply
L,

Offline

Board footer

Moderator control

Enjoy the 24th of April
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support