Complex content type

38 views
Skip to first unread message

Said Akhmedbayev

unread,
Apr 29, 2016, 12:51:45 AM4/29/16
to Django users
Need to build a content type that accepts inputs from users. Those inputs must be stored in database alone with information who entered it (user's information like user name). 

Here is what I am looking to build:



 Here is how my models.py looks at the moment. 

from django.contrib.auth.models import User
from django.db import models


# Create your models here.
class Unit(models.Model):
    unit_number = models.PositiveIntegerField(blank=True, null=True, default=1)
    title = models.CharField(max_length=150, blank=True, null=True)
    scripture_ref = models.CharField(max_length=250, blank=True, null=True)

    def __str__(self):
        return self.title


class Subsection(models.Model):
    title = models.CharField(max_length=150, blank=True, null=True)
    subsection_text = models.TextField(default='SUBSECTION TEXT')
    unit = models.ForeignKey(Unit, on_delete=models.CASCADE)


class Question(models.Model):
    question_text = models.CharField(max_length=2000, default='QUESTION TEXT')
    subsection = models.ForeignKey(Subsection, on_delete=models.CASCADE)
    
    
class Answer(models.Model):
    answer = models.TextField(blank=True, null=True) # this field I want to store alone with question_text and answered_by fields
    answered_by = models.ForeignKey(User, blank=True, null=True, related_name='answers')

I am probably asking a lot, but I hope that there will be someone who can help. My question is what should I do with models.py, forms.py and views.py to put together this kind of content type?

Thank you in advance!

Avraham Serour

unread,
May 1, 2016, 10:06:51 AM5/1/16
to django-users
what's so special about your content type? It seems you are just storing text.


--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/69ca76f4-a839-4113-902a-89e4b0cb0d6b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Said Akhmedbayev

unread,
May 2, 2016, 2:23:29 AM5/2/16
to Django users
Avraham, I understand that the content type is not any special or complex for you. Probably, I should wrote "complex content type for me". Anyways, can you provide any instructions for how can make this kind of content type?

ludovic coues

unread,
May 2, 2016, 5:25:06 AM5/2/16
to django...@googlegroups.com
I have some trouble trying to understand what you means by "content type". That terms refer to the content of a file. For exemple, text/plain for plain text file or image/png for an image encoded as png.
What you show look like a standard form. If you need basic pointer on how to use a form, saving it's data along with current logged-in user, you might want to have a look at the django girls tutorial. Specifically, http://tutorial.djangogirls.org/en/django_forms/ is about django forms.

Said Akhmedbayev

unread,
May 2, 2016, 5:57:56 AM5/2/16
to Django users
Thank you ludovic and sorry for not being able to clearly explain what I want to do :-(. You are right, I want to show a user a simple django form, but I cannot figure out how to save an individual answer alone with its correspondence question's id and user's id

So my table for "Answer" model will look like:

user_id       question_id             answer
     01                    01                 text of an answer
     01                    02                 text of an answer
     02                    01                 text of an answer
     02                    02                 text of an answer
     03                    01                 text of an answer
     03                    02                 text of an answer

ludovic coues

unread,
May 2, 2016, 6:25:13 AM5/2/16
to django...@googlegroups.com
You might want to add a foreign key in the answer model pointing to
the question.
Then in the view to show the form, create an answer object with it's
question FK assigned to the question and create a form from that
answer. In the view processing the form, you should get that object
back, with the question and the text filled. Complete it with the
user, as shown in the django girls tutorial and save your answer.
> --
> 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 https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/fdbc2c01-2059-406d-93fe-762d9fdc4a73%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

Said Akhmedbayev

unread,
May 2, 2016, 9:48:36 AM5/2/16
to Django users
Ludovic, would you be so kind to help me and put together some example code of what I should  place in the forms.py and the views.py? I would greatly appreciate.

ludovic coues

unread,
May 2, 2016, 12:14:57 PM5/2/16
to django...@googlegroups.com
There is such example in the django girls tutorial I linked earlier.
> https://groups.google.com/d/msgid/django-users/c96cece4-7a3b-45fc-bb6f-5dfdd1e1b242%40googlegroups.com.

Aaron Cannon

unread,
May 2, 2016, 2:58:54 PM5/2/16
to django...@googlegroups.com
To your Answer class, you could just add a field like:

question = models.ForeignKey(Question, on_delete=models.CASCADE)

It may be as simple as that.

Does that help?

Aaron
> https://groups.google.com/d/msgid/django-users/CAEuG%2BTZ0nZMxtBroPkriKuE2e0YBH%2BKPQ5uEkPR1f5CMX0bO4g%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages