social-links-field
is a Django package that provides a custom model field, form field, and widget for managing social media links with ease. It allows developers to store and manage multiple social media links for a model with built-in validation and a user-friendly interface.
- Custom JSON-based field for storing social media links
- Predefined social media platforms
- Dynamic form widget for adding/removing social links
- Built-in validation
- Easy integration with Django forms and admin interface
Install the package using pip:
pip install django-social-links-field
In your Django project's settings.py
:
INSTALLED_APPS = [
...
'social_links_field',
...
]
from django.db import models
from social_links_field.models import SocialLinksField
class UserProfile(models.Model):
name = models.CharField(max_length=100)
social_links = SocialLinksField()
The field automatically works with Django admin:
from django.contrib import admin
from .models import UserProfile
@admin.register(UserProfile)
class UserProfileAdmin(admin.ModelAdmin):
# No special configuration needed
pass
- GitHub
- YouTube
- Custom
Configure the downdown values from settings.py
SOCIAL_LINKS_FIELD_MEDIA_TYPES = [
# value, label
("facebook", "Facebook"),
("instagram", "Instagram"),
("twitter", "Twitter"),
("linkedin", "LinkedIn"),
("github", "GitHub"),
("youtube", "YouTube"),
]
Social links are stored as a list of dictionaries:
[
{
'type': 'facebook',
'link': 'example_user',
'label': 'My Facebook Profile'
}
]
from django import forms
from .models import UserProfile
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ['name', 'social_links']
You can extend SOCIAL_MEDIA_TYPES
in your project:
from social_links_field.models import SOCIAL_MEDIA_TYPES
SOCIAL_MEDIA_TYPES += [
('tiktok', 'TikTok'),
('telegram', 'Telegram')
]
You can customize html/css/js by directly overriding default widget html template
social_links_field/social_links_widget.html
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License