Skip to content

Commit 8818cdf

Browse files
committed
add 1:n relation User to UserExtension
1 parent 3bf9e8d commit 8818cdf

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

testapp/forms/user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
class UserForm(ModelForm):
1212
class Meta:
1313
model = User
14-
exclude = ['username', 'password', 'last_login', 'date_joined']
14+
exclude = ['password', 'last_login', 'date_joined']
1515
widgets = {
1616
'password': PasswordInput,
1717
'groups': SelectizeMultiple,

testapp/migrations/0001_initial.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,14 @@ class Migration(migrations.Migration):
164164
('phone_number', models.CharField(blank=True, max_length=25, null=True, verbose_name='Phone Number')),
165165
],
166166
),
167+
migrations.CreateModel(
168+
name='UserExtension',
169+
fields=[
170+
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
171+
('phone_number', models.CharField(blank=True, max_length=25, null=True, verbose_name='Phone Number')),
172+
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='user_extensions', to='testapp.user')),
173+
],
174+
),
167175
migrations.CreateModel(
168176
name='Gallery',
169177
fields=[

testapp/models/user.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,17 @@ class ExtendUser(models.Model):
5353
blank=True,
5454
null=True,
5555
)
56+
57+
58+
class UserExtension(models.Model):
59+
user = models.ForeignKey(
60+
User,
61+
on_delete=models.CASCADE,
62+
related_name='user_extensions',
63+
)
64+
phone_number = models.CharField(
65+
verbose_name="Phone Number",
66+
max_length=25,
67+
blank=True,
68+
null=True,
69+
)

testapp/views.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,8 @@ class DemoFormCollectionView(DemoFormCollectionViewMixin, FormCollectionView):
281281
pass
282282

283283

284-
class UserCollectionView(DemoFormCollectionViewMixin, EditCollectionView):
285-
model = User
286-
template_name = 'testapp/form-collection.html'
287-
288-
def get_object(self, queryset=None):
289-
user, _ = self.model.objects.get_or_create(username='demo')
290-
return user
284+
class DemoModelCollectionView(DemoFormCollectionViewMixin, SessionFormCollectionViewMixin, EditCollectionView):
285+
pass
291286

292287

293288
class CompanyCollectionView(DemoFormCollectionViewMixin, SessionFormCollectionViewMixin, EditCollectionView):
@@ -726,9 +721,10 @@ class CompleteForm(FormMixin, forms.Form):
726721
), name='poll'),
727722
path('company', CompanyCollectionView.as_view(), name='company'),
728723
path('companies', CompaniesCollectionView.as_view(), name='company'),
729-
path('user', UserCollectionView.as_view(
730-
collection_class=UserCollection
731-
), name='user'),
724+
path('user-collection', DemoModelCollectionView.as_view(
725+
collection_class=UserCollection,
726+
model=User,
727+
), name='user-collection'),
732728
# path('userlist', UserCollectionView.as_view(
733729
# collection_class=UserListCollection
734730
# ), name='userlist'),

0 commit comments

Comments
 (0)