User Created Objects (models)

58 views
Skip to first unread message

Ideo Rex

unread,
Oct 20, 2013, 8:24:22 PM10/20/13
to django...@googlegroups.com
Hello,
I'm relatively new to Django. So I have a working (local) web application. I can create new model objects from the admin site, but I would like my users to be able to create their own objects and save them to the database. I'm really confused on the over arching process on how this is down and I would appreciate it if someone could lead me in the correct direction.

I want to allow my users:
1. fill in information on a page
2. submit this page to the database
3. redirect them to a page that reflects their input ("You successfully submitted this {{title}}")

Thanks

Sergiy Khohlov

unread,
Oct 21, 2013, 1:45:12 AM10/21/13
to django-users
Task is really simple :
Take a look at https://docs.djangoproject.com/en/dev/topics/auth/default/
(this a good start for novice)
Use UserCreationForm for passing data from user and validating
If form is correct save new user
After this you can redirect to new page
Many thanks,

Serge


+380 636150445
skype: skhohlov
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/a13ae0e4-ca48-4908-869c-d22589361032%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Daniel Roseman

unread,
Oct 21, 2013, 5:55:33 AM10/21/13
to django...@googlegroups.com
This is indeed a very simple request. And it is fully covered in the documentation about forms: see https://docs.djangoproject.com/en/1.5/topics/forms/ for an introduction, and https://docs.djangoproject.com/en/1.5/topics/forms/modelforms/ for the specifics about saving form data to a model instance.

(The other answer to this question, from Sergiy, seems to have completely missed the point with his link to the auth docs, unfortunately.) 
--
DR.

Ideo Rex

unread,
Oct 21, 2013, 9:36:15 PM10/21/13
to django...@googlegroups.com
Thanks for the link. I'm spent a good part of the day trying to figure it out to no avail. My next approach was to describe how I walked through it and see where I went wrong.

I went into my models.py

from django.db import models

from django.core.urlresolvers import reverse

from datetime import *

from django.forms import ModelForm


# Create your models here.


class Ride(models.Model):

    title = models.CharField(max_length=255)

    speed = models.CharField(max_length=255)

    discipline = models.CharField(max_length=255)

    slug = models.SlugField(unique=True, max_length=255)

    description = models.CharField(max_length=255)

    scheduletime = models.DateTimeField('date published')

    lat = models.FloatField(blank=True, null=True)

    lon = models.FloatField(blank=True, null=True)

    published = models.BooleanField(default=True)

    created = models.DateTimeField(auto_now_add=True)

    


    

    class Meta:

        ordering = ['-created']

    

    def __unicode__(self):

        return u'%s' % self.title

    

    def get_absolute_url(self):

        return reverse('blog.views.post', args=[self.slug])


class RideForm(ModelForm):

    class Meta:

        model = Ride

        fields = ('title', 'speed', 'discipline', 'description', 'scheduletime', 'lat', 'lon')


I became really confused after this point. It is unclear to me how I cause a html site to produce a request and for it to make its way to this model. 



Approach #2

I tried to solve this another way. I looked at the polls-tutorial which I have done before. I tried to replicate their vote function in views.py. The challenge that I found was that I struggled to make a unique object on the response of the program. Does anyone have any tips for making this happen. Thanks

Pitchblack

unread,
Sep 16, 2014, 12:02:47 AM9/16/14
to django...@googlegroups.com
Is there a way to add the user to the data model, so it knows who created the record and who updated it last?

Mike Dewhirst

unread,
Sep 16, 2014, 9:58:38 AM9/16/14
to django...@googlegroups.com
On 16/09/2014 2:02 PM, Pitchblack wrote:
> Is there a way to add the user to the data model, so it knows who
> created the record and who updated it last?

You need to establish a couple of foreign key fields in your model (eg
created and updated) which both link to auth.user. Then grab
request.user at the appropriate time and do <model>.created =
request.user and <model>.updated = request.user just before saving.

This can be done in the admin
https://docs.djangoproject.com/en/1.6/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model


In your own forms it is the same as updating any other fields.

hth

>
> On Sunday, October 20, 2013 5:24:22 PM UTC-7, Ideo Rex wrote:
>
> Hello,
> I'm relatively new to Django. So I have a working (local) web
> application. I can create new model objects from the admin site, but
> I would like my users to be able to create their own objects and
> save them to the database. I'm really confused on the over arching
> process on how this is down and I would appreciate it if someone
> could lead me in the correct direction.
>
> I want to allow my users:
> 1. fill in information on a page
> 2. submit this page to the database
> 3. redirect them to a page that reflects their input ("You
> successfully submitted this {{title}}")
>
> Thanks
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/e5e91694-3637-4fd9-8c3e-0edb4606477e%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/e5e91694-3637-4fd9-8c3e-0edb4606477e%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Michael Martin

unread,
Sep 16, 2014, 1:23:22 PM9/16/14
to django...@googlegroups.com
Mike,

Thanks for responding.

I seem to be getting an error when I try to make foreign keys in my model.  Do you have any idea why?

My Model that I am adding is called GeneralSetting

I am now importing:
from django.db import models
from django.contrib.auth.models import User

I added under my
class GeneralSetting(models.Model):
    updated_by=models.ForeignKey(User)
    created_by=models.ForeignKey(User)

    def __unicode__(self):
        return "General Settings"




C:\Users\mike\workspace\ciscoebondinggateway>jython manage.py makemigrations

←[31;1mCommandError: System check identified some issues:

ERRORS:
←[31;1msettings.GeneralSetting.created_by: (fields.E304) Reverse accessor for 'G
eneralSetting.created_by' clashes with reverse accessor for 'GeneralSetting.upda
ted_by'.
        HINT: Add or change a related_name argument to the definition for 'Gener
alSetting.created_by' or 'GeneralSetting.updated_by'.←[0m
←[31;1msettings.GeneralSetting.updated_by: (fields.E304) Reverse accessor for 'G
eneralSetting.updated_by' clashes with reverse accessor for 'GeneralSetting.crea
ted_by'.
        HINT: Add or change a related_name argument to the definition for 'Gener
alSetting.updated_by' or 'GeneralSetting.created_by'.←[0m
←[0m
C:\Users\mike\workspace\ciscoebondinggateway>





To post to this group, send email to django...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.

Mike Dewhirst

unread,
Sep 16, 2014, 6:44:41 PM9/16/14
to django...@googlegroups.com
On 17/09/2014 3:22 AM, Michael Martin wrote:
> Mike,
>
> Thanks for responding.
>
> I seem to be getting an error when I try to make foreign keys in my
> model. Do you have any idea why?
>
> My Model that I am adding is called GeneralSetting
>
> I am now importing:
> from django.db import models
> from django.contrib.auth.models import User
>
> I added under my
> class GeneralSetting(models.Model):
> Â Â Â updated_by=models.ForeignKey(User)
> Â Â Â created_by=models.ForeignKey(User)
>
> Â Â Â def __unicode__(self):
> Â Â Â Â Â Â Â return "General Settings"
>
>
>
>
> C:\Users\mike\workspace\ciscoebondinggateway>jython manage.py makemigrations
>
> ↠[31;1mCommandError: System check identified some issues:
>
> ERRORS:
> ↠[31;1msettings.GeneralSetting.created_by: (fields.E304) Reverse
> accessor for 'G
> eneralSetting.created_by' clashes with reverse accessor for
> 'GeneralSetting.upda
> ted_by'.
> Â Â Â Â Â Â Â HINT: Add or change a related_name argument to the
> definition for 'Gener
> alSetting.created_by' or 'GeneralSetting.updated_by'.↠[0m
> ↠[31;1msettings.GeneralSetting.updated_by: (fields.E304) Reverse
> accessor for 'G
> eneralSetting.updated_by' clashes with reverse accessor for
> 'GeneralSetting.crea
> ted_by'.
> Â Â Â Â Â Â Â HINT: Add or change a related_name argument to the
> definition for 'Gener
> alSetting.updated_by' or 'GeneralSetting.created_by'.↠[0m
> ↠[0m
> C:\Users\mike\workspace\ciscoebondinggateway>

It appears that you need a related_name argument for one or both of
those foreign keys. The Django documentation is probably the best part
of Django. Have a look at https://docs.djangoproject.com/en/1.6/ which
is the entry page. Then in the model layer look under Models and click
the Field types link. When that page comes up scroll down a little to
see Relationship fields in the right-sidebar. The link you want is
Arguments. Alternatively, you could search for related_name but I think
you would be better served as a beginner to start at the entry page and
to follow your nose from there.

Good luck

Mike



>
>
>
>
> On Tue, Sep 16, 2014 at 6:57 AM, Mike Dewhirst <mi...@dewhirst.com.au
> <mailto:mi...@dewhirst.com.au>> wrote:
>
> On 16/09/2014 2:02 PM, Pitchblack wrote:
>
> Is there a way to add the user to the data model, so it knows who
> created the record and who updated it last?
>
>
> You need to establish a couple of foreign key fields in your model
> (eg created and updated) which both link to auth.user. Then grab
> request.user at the appropriate time and do <model>.created =
> request.user and <model>.updated = request.user just before saving.
>
> This can be done in the admin
> https://docs.djangoproject.__com/en/1.6/ref/contrib/admin/#__django.contrib.admin.__ModelAdmin.save_model
> <https://docs.djangoproject.com/en/1.6/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model>
>
>
> In your own forms it is the same as updating any other fields.
>
> hth
>
>
> On Sunday, October 20, 2013 5:24:22 PM UTC-7, Ideo Rex wrote:
>
> Â Â Hello,
> Â Â I'm relatively new to Django. So I have a working (local) web
> Â Â application. I can create new model objects from the admin
> site, but
> Â Â I would like my users to be able to create their own
> objects and
> Â Â save them to the database. I'm really confused on the over
> arching
> Â Â process on how this is down and I would appreciate it if
> someone
> Â Â could lead me in the correct direction.
>
> Â Â I want to allow my users:
> Â Â 1. fill in information on a page
> Â Â 2. submit this page to the database
> Â Â 3. redirect them to a page that reflects their input ("You
> Â Â successfully submitted this {{title}}")
>
> Â Â Thanks
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from
> it, send
> an email to django-users+unsubscribe@__googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>
> <mailto:django-users+...@googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>>.
> To post to this group, send email to
> django...@googlegroups.com <mailto:django...@googlegroups.com>
> <mailto:django-users@__googlegroups.com
> <mailto:django...@googlegroups.com>>.
> Visit this group at
> http://groups.google.com/__group/django-users
> <http://groups.google.com/group/django-users>.
> To view this discussion on the web visit
> https://groups.google.com/d/__msgid/django-users/e5e91694-__3637-4fd9-8c3e-0edb4606477e%__40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/e5e91694-3637-4fd9-8c3e-0edb4606477e%40googlegroups.com>
> <https://groups.google.com/d/__msgid/django-users/e5e91694-__3637-4fd9-8c3e-0edb4606477e%__40googlegroups.com?utm_medium=__email&utm_source=footer
> <https://groups.google.com/d/msgid/django-users/e5e91694-3637-4fd9-8c3e-0edb4606477e%40googlegroups.com?utm_medium=email&utm_source=footer>>.
> For more options, visit https://groups.google.com/d/__optout
> <https://groups.google.com/d/optout>.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to django-users+unsubscribe@__googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at http://groups.google.com/__group/django-users
> <http://groups.google.com/group/django-users>.
> To view this discussion on the web visit
> https://groups.google.com/d/__msgid/django-users/541841E5.__3010703%40dewhirst.com.au
> <https://groups.google.com/d/msgid/django-users/541841E5.3010703%40dewhirst.com.au>.
>
> For more options, visit https://groups.google.com/d/__optout
> <https://groups.google.com/d/optout>.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAD0urK1J4L4ovc8oh5bUJ_xKhF9a-oe1YgtPoJOOJLm2R4h68A%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAD0urK1J4L4ovc8oh5bUJ_xKhF9a-oe1YgtPoJOOJLm2R4h68A%40mail.gmail.com?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
0 new messages