New to Django - How to add inline object to the parent object via shell command or scripts

196 views
Skip to first unread message

pha...@gmail.com

unread,
Apr 5, 2014, 4:21:38 AM4/5/14
to django...@googlegroups.com
I am trying to learn how to use Django.

I created 2 very simple models:

models.py

class Course(models.Model):
    name = models.CharField(max_length=100)
    instructor = models.CharField(max_length=100)

    student = models.ForeignKey('Student', null=True, blank=True)

    def __str__(self):
        return (self.name)

class Student(models.Model):
    name = models.CharField(max_length=100)
    courses = RelatedObjectsDescriptor()

    def __str__(self):
        return (self.name)

forms.py

class CourseInline(admin.TabularInline):
    model = Course
    form = CourseForm
    extra = 3

class StudentAdmin(admin.ModelAdmin):
    form = StudentForm
    search_fields = ('name', )
    fields = ('name', )
    ordering = ('name',)

    inlines = [CourseInline]
 
admin.site.register(Student, StudentAdmin)


with this setup, I can add student then add courses to the student.

What I am trying to do is to use script to enter the known records:

student = Student(name ='student1')                                                                    - This work
course = Course(name='Math101", instructor="Smith", student=student)                - This work, but when I view the student record via admin page, the course does not show up as an inline object

My problem id how to add the courses to this student as I add them through the admin page.

Thank you very much for any help.


Daniel Roseman

unread,
Apr 5, 2014, 12:07:00 PM4/5/14
to django...@googlegroups.com
On Saturday, 5 April 2014 09:21:38 UTC+1, H. Pham wrote:
I am trying to learn how to use Django.

Then you should read the tutorial. 

 
I created 2 very simple models:

models.py

class Course(models.Model):
    name = models.CharField(max_length=100)
    instructor = models.CharField(max_length=100)

    student = models.ForeignKey('Student', null=True, blank=True)

This is wrong. This means each course can only have one student. The ForeignKey should be on Student, pointing to Course. Or, more likely, you should have a ManyToManyField, since Courses can have many students, and Students can have many classes.
 
    def __str__(self):
        return (self.name)

class Student(models.Model):
    name = models.CharField(max_length=100)
    courses = RelatedObjectsDescriptor()

Is this from the genericm2m project? If so, you should say so. But there's no need for it here: a simple ManyToManyField is what you need.
 
    def __str__(self):
        return (self.name)

forms.py

class CourseInline(admin.TabularInline):
    model = Course
    form = CourseForm
    extra = 3

class StudentAdmin(admin.ModelAdmin):
    form = StudentForm
    search_fields = ('name', )
    fields = ('name', )
    ordering = ('name',)

    inlines = [CourseInline]
 
admin.site.register(Student, StudentAdmin)

This stuff should be in admin.py
 
 
with this setup, I can add student then add courses to the student.

What I am trying to do is to use script to enter the known records:

student = Student(name ='student1')                                                                    - This work
course = Course(name='Math101", instructor="Smith", student=student)                - This work, but when I view the student record via admin page, the course does not show up as an inline object


You've confused yourself by trying to use that genericm2m project. Stick to the standard classes for now.

    course = Course.objects.create(name="math101', instructor='Smith')
    course.students.add(student)

This is all well covered in the tutorial, which you should read rather than relying on random blog posts.

--
DR. 

H. Pham

unread,
Apr 6, 2014, 12:38:57 PM4/6/14
to django...@googlegroups.com
Apr 5

Daniel, Thank you very much for your response.  I have been reading the tutorial and practicing.  This is my practicing project.

With your help, I make a couple adjustments and have it working.

Thanks again,
Reply all
Reply to author
Forward
0 new messages