Tuesday, September 16, 2014

Re: User Created Objects (models)

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>




On Tue, Sep 16, 2014 at 6:57 AM, Mike Dewhirst <miked@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

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+unsubscribe@googlegroups.com>.
To post to this group, send email to django-users@googlegroups.com
<mailto:django-users@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.

--
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-users@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/541841E5.3010703%40dewhirst.com.au.

For more options, visit 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.
To post to this group, send email to django-users@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.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment