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)


-- 
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 [email protected].
To post to this group, send email to [email protected].
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/31fbb3e8-20d9-4f5f-879d-97ea8e8bc760%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to