How to add inline object to parent object using django shell command or scripts

140 views
Skip to first unread message

H. Pham

unread,
Apr 5, 2014, 9:25:07 AM4/5/14
to django...@googlegroups.com
I am very new to Django.  I have followed the tutorial and instruction in document and create a couple models: Student and Course.

Student has Course as "RelatedObjectsDescriptor"
Course has Student as ForeignKey

Course is TabularInline object of Student.

I was able to create Student object and add Courses Objects to student in Admin page succeessfully.

Since I have a lot of student records and each student have number of course assigned.  I would like to create a python script either using Sqlite interface or object in view to populate the database with these records.

What I have done are the following:

student = Student.objects.create(...)

for each course:
    course = Course.objects.create(...)
    student.courses.add(course)
    student.courses.connect(course)
When I view the database,  all students were added and all the course were added to the appropriate student;  However when I view in record in the admin page (change/update), none of the courses show as inline objects.

If I add the course to the student via admin page, they all show up correctly.

Any help is greatly appreciated.


I am using Django 1.6

with the following APPs
    'autocomplete_light',
    'navigation_autocomplete',
    'genericm2m',

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 StudentForm(autocomplete_light.ModelForm):
    class Meta:
        model = Student
        exclude = ()

class CourseForm(autocomplete_light.ModelForm):
    class Meta:
        model = Course
        exclude = ()
admin.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)

autocomplete_light_registry.py

class StudentAutocomplete(autocomplete_light.AutocompleteGenericBase):
    choices = Student.objects.all()
    search_fields = ('name',)

    autocomplete_js_attributes = {'placeholder': 'suggestions...', 'minimum_characters': 0}
autocomplete_light.register(StudentAutocomplete)


class CourseAutocomplete(autocomplete_light.AutocompleteGenericBase):
    choices = Course.objects.all()
    search_fields = ('name', 'instructor')

    autocomplete_js_attributes = {'placeholder': 'suggestions...', 'minimum_characters': 0}
autocomplete_light.register(CourseAutocomplete)


Reply all
Reply to author
Forward
0 new messages