How to get self.id when saving a new object?

2,915 views
Skip to first unread message

Andre Lopes

unread,
Jan 9, 2013, 7:55:17 AM1/9/13
to django...@googlegroups.com
Hi,

I'm without a clue on how to get the sef.id when creating a new object
in Django. I've created a question in StackOverflow. Can someone give
me a clue on this subject?

Please read the question:
http://stackoverflow.com/questions/14234917/django-how-to-get-self-id-when-saving-a-new-object

Any clues?


Best Regards,

Tom Evans

unread,
Jan 9, 2013, 9:33:47 AM1/9/13
to django...@googlegroups.com
Save the object first.

Cheers

Tom

Sergiy Khohlov

unread,
Jan 9, 2013, 9:39:10 AM1/9/13
to django...@googlegroups.com
I have no idea why you decided to investigate a bicycle...

Please separate task in two simple :
1) for editing object use url /<objectname>/<id>/edit
2) for creating new object /<objectname>/new

Take a look at generic view.

Many thanks,

Serge


+380 636150445
skype: skhohlov


2013/1/9 Tom Evans <teva...@googlemail.com>:
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

Sergiy Khohlov

unread,
Jan 9, 2013, 11:04:50 AM1/9/13
to django...@googlegroups.com
I spend some time for this one and propose next solution :

At first your model has only one image. in this case you dont need
to add url() method . Now model is looks like


from django.db import models
from mptt.models import MPTTModel, TreeForeignKey
# Create your models here.
class BicycleAdItemKind(MPTTModel):
def item_kind_image(self):
return '<img align="middle" src="/media/%s" height="60px" />'
% self.image
item_kind_image.allow_tags = True

# Bicicleta completa, Componentes para bicicleta, Acessorios para ciclista
n_item_kind = models.CharField(max_length=50)
parent = TreeForeignKey('self', null=True,
blank=True, related_name='children')
description = models.TextField(null=True, blank=True)
image = models.ImageField(upload_to='Images',
null=True, blank=True)
date_inserted = models.DateTimeField(auto_now_add=True)
date_last_update = models.DateTimeField(auto_now=True)

def __unicode__(self):
return self.n_item_kind

class MPTTMeta:
order_insertion_by = ['n_item_kind']


Also view should be simple :

# Create your views here.
from django.views.generic import CreateView, ListView
from newmodel.models import BicycleAdItemKind
from newmodel.form import CreatenewmodelForm

class newmodelListView(ListView):
model = BicycleAdItemKind
template_name = 'newmodel/index.html'

class newmodelAddView(CreateView):
model = BicycleAdItemKind
template_name = 'newmodel/create.html'
form_class = CreatenewmodelForm
# please update form valid method
def form_valid(self, form, **kwargs):
BicycleAdItemKind.objects.create()


And your urls.py

from django.conf.urls import patterns, include, url
from newmodel.views import newmodelAddView, newmodelListView

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
# Examples:
# url(r'^$', 'andrelopes.views.home', name='home'),
# url(r'^andrelopes/', include('andrelopes.foo.urls')),

url(r'^newmodel/create$', newmodelAddView.as_view()),
url(r'^newmodel/$', newmodelListView.as_view()),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
)


You should create two templates only it is not hard. I think that
form usage is over the top and you can skip it

Many thanks,

Serge


+380 636150445
skype: skhohlov


2013/1/9 Sergiy Khohlov <skho...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages